Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
while read newfile <&3; do   
 if [[ ! $newfile =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
 #
 while read oldfile <&3; do   
 if [[ ! $oldfile =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
    echo Comparing "$newfile" with "$oldfile"
    #
    if diff "$newfile" "$oldfile" >/dev/null ; then
      echo The files compared are the same. No changes were made.
    else
        echo The files compared are different.
    fi    
 done 3</infanass/dev/admin/oldfiles.txt
done 3</infanass/dev/admin/newfiles.txt

I think this is the right way to do nested loops.. but it doesnt quite work right.

share|improve this question
    
cmp $1 $2 is also more efficient than diff $1 $2. –  Lekensteyn Jul 10 '13 at 20:32
    
diff -q $1 $2 is even more efficient. –  Evan Teitelman Jul 10 '13 at 20:36
    
@mkrouse: Could you please be more clear about what you expect the script to do and what it does instead? –  Evan Teitelman Jul 10 '13 at 20:37
    
Please explain what you want it to do and what it is doing. –  terdon Jul 10 '13 at 20:40
add comment

closed as unclear what you're asking by Evan Teitelman, terdon, jasonwryan, slm, Anthon Jul 11 '13 at 6:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 3 down vote accepted

You don't have to use file descriptor 3 like that.

while read newfile do   
    if [[ ! $newfile =~ [^[:space:]] ]] ; then  #empty line exception
       continue
    fi   

    while read oldfile ; do   
       if [[ ! $oldfile =~ [^[:space:]] ]] ; then  #empty line exception
          continue
       fi   
       echo Comparing "$newfile" with "$oldfile"

       # diff -q doesn't bother generating a diff.
       # It just tells you whether or not the files match.
       if diff -q "$newfile" "$oldfile" >/dev/null ; then
         echo The files compared are the same. No changes were made.
       else
           echo The files compared are different.
       fi    
    done < /infanass/dev/admin/oldfiles.txt
done < /infanass/dev/admin/newfiles.txt

Assuming an empty line is a line with only whitespace on it, the empty line exception code may match non-empty lines. This will match lines with only whitespace on them (remove \s* to make it only match entirely empty lines):

if [[ ! $newfile =~ ^\s*$ ]] ; then  #empty line exception
share|improve this answer
    
Using the file descriptor 3 doesn't hurt either.. I am planning to ssh to different servers in the script. –  mkrouse Jul 11 '13 at 12:53
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.