Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a file using shell script

Tags:

linux

shell

I have a text file named sqlfile, with the following content:

a.sql
b.sql
c.sql
d.sql

What I want is that to store them in variables and then print using for loop. But here I get only d.sql in the output of the script.

The script:

#!/bin/bash

while read line
do
files=`echo $line`
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile

for file in $files
        do
                echo $file
        done
like image 241
Abdul Manaf Avatar asked Dec 21 '22 03:12

Abdul Manaf


1 Answers

A variable can only hold one element, what you want is an array

#!/bin/bash

while read line
do
  files+=( "$line" )
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile

for file in "${files[@]}"
do
  echo "$file"
done
like image 58
SiegeX Avatar answered Dec 24 '22 01:12

SiegeX