Bash script to read data from a file into an array

I was asked to write a small shell script which can read the difference of two files (which extracts filenames) and pass the extracted filenames to another shell script. It is not too complicated but posting it for other people’s benefit.
Here is a quick explanation.
Line 5 takes a difference of files file1.txt and file2.txt, cuts the output from characters 3 to 13, and writes it into a file.
Line 7 opens temporarylist.txt as file descriptor 3 for reading.
Line 9 runs a loop till there is nothing further to read from file descriptor 3.
Line 11 passes the read data to a different shell script (named othershellscript in this case).
Line 15 closes the file descriptor 3.

cd /home/masud
diff file1.txt file2.txt | cut -c3-13 > temporarylist.txt
exec 3< “temporarylist.txt” || exit 1
while read i <&3; do
scripts/othershellscript $i
exec 3<&-

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar