Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

bash - 2 while read loops?

So the object of the script I'm making is to compare files from two while read lists that have file path names in them...

while read compareFile <&3; do     
 if [[ ! $server =~ [^[:space:]] ]] ; then  #empty line exception
  continue
 fi   
    echo "Comparing file - $compareFile"
 if diff "$compareFile" _(other file from loop?_) >/dev/null ; then
    echo Same
 else
     echo Different
 fi   
 done 3</infanass/dev/admin/filestoCompare.txt

I need to be able to compare files from two different lists at the same time through two while read loops... Is this even possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I would restructure that along these lines:

while true
do
   read -u3 line1 || break
   read -u4 line2 || break

   # do whatever...
done 3< file1 4< file2

That uses a single loop, and will exit that loop when end of file is reached on either input file. The logic would be a little more complicated if you want to read both files entirely, even if one ends early...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...