Passing a variable via ssh, can't quite get it right

Hi Guys n Girls,

Below im using a while command to wait for a file on another server then carrying on with the script.....
I dont believe the $Sausage1 variable is being passed to the other server so its not finding the file. If i replace the variable with the date then it works as expected.

Could someone please help with the correct layout of the command using the variable?

Sausage1=`date '+%Y%m%d'`
ssh transfer@theserver 'while [ ! -f /the/path/tofile/filename_out.bin_"$Sausage1"_?????? ]; do sleep 120; done'

Shell variables are not expanded when they appear in a string quoted by single-quotes. Since there are no spaces in the output from that date command, you could just use:

Sausage1=`date '+%Y%m%d'`
ssh transfer@theserver "while [ ! -f /the/path/tofile/filename_out.bin_$Sausage1_?????? ]; do sleep 120; done"

In cases where the variable you are expanding might contain spaces you can escape the inner double-quotes as in:

Sausage1=`date '+%Y%m%d'`
ssh transfer@theserver "while [ ! -f /the/path/tofile/filename_out.bin_\"$Sausage1\"_?????? ]; do sleep 120; done"

or, if you aren't using an pure Bourne shell, the preferred form:

Sausage1=$(date '+%Y%m%d')
ssh transfer@theserver "while [ ! -f /the/path/tofile/filename_out.bin_\"$Sausage1\"_?????? ]; do sleep 120; done"

Note also that this script will fail if the filename matching pattern filename_out.bin_"$Sausage1"_?????? ever matches more than one file.

1 Like
Sausage1=$(date '+%Y%m%d')
ssh transfer@theserver "while [ ! -f /the/path/tofile/filename_out.bin_\"$Sausage1\"_?????? ]; do sleep 120; done"

By the gods! This did it......i was pulling out what little hair i have left. Brilliant! thank you for your clear explanation and explaining the errors in my ways :slight_smile: