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.

About troyski

I'm a freelance UNIX engineer working in the UK. I'm married to Tina and between us we have six children. I'm a bit of an Apple fan boy, and all the Windows machines in the house are a thing of the past now.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Private

Post navigation