use vimdiff inside while read

Hi,

I need to read a file name from a txt file, found the difference in two folders, then modify the file using vimdiff,

while read file
do
 diff src/$file dest/$file > /dev/null 2>&1
 if [ $? -eq 0]; then
    vimdiff src/$file dest/$file 
fi
done < ./my_text_file

since the stdin has been redirect to file my_text_file, vimdiff can't work properly.

anyone can help me to fix it?

thanks.

peter

Try this:

while IFS= read -r; do
  diff src/"$REPLY" dest/"$REPLY" || ( 
    exec 0< /dev/tty
    vimdiff src/"$REPLY" dest/"$REPLY" 
    )
done < infile
2 Likes

works like a charm

------------------------------------

could anyone please explain how this script work ?

It redirects the standard input to the controlling terminal inside the loop:

exec 0< /dev/tty

In this way the program vimdiff reads the user input instead of the stream coming from the pipe.

Thanks,

Could you please explain the significance of the diff command as well

diff src/"$REPLY" dest/"$REPLY" ||
 ...

This is a logical OR:

command1 || command2

In this case command2 will be executed only if command1 returns status different than 0 (i.e. if the files to be compared are different).

1 Like