Bit of a gotcha this one.
If you have a ssh call within a while loop, ssh will utilise the contents of the loop variable in one go and thus the loop will exit on the first iteration.
For example
/tmp/file
has two filenames in it, file1.txt
and file2.txt
. If you write the following: –
infile=/tmp/file remotehost=192.168.1.1 cat ${infile} | while read file do echo "processing $file" ssh $remotehost "ls $file" done |
The script will only process the first line of /tmp/file
. If you comment out the ssh and re-run, you will see both lines are processed.
The solution is to use a for loop instead of the while loop. i.e.
for transfer in $(cat $infile) |
This works correctly.