TristanMatthews
TristanMatthews

Reputation: 2571

Strange bash errors when passing script to ssh

I'm trying to run a local script remotely that I'm calling with some other code. The basic formate is

ssh -i ${PemKey} ${User}@${URL} 'bash -s' -- < ${Command}

I get the error line 24: ${Command}: ambiguous redirect Command is a string with the name of the script I want to run and its arguments. If I change the script to just print the command as

echo "ssh -i ${PemKey} ${User}@${URL} 'bash -s' -- < ${Command}"

and then run the command myself it works just fine. I've tried putting the command in a temp variable and then call it that way, like:

TEMP="ssh -i ${PemKey} ${User}@${URL} 'bash -s' -- < ${Command}"
$TEMP
echo $TEMP

This results in No such file or directory. Again the echoed version of the command runs just fine at the command line.

Anyone know what I'm doing wrong here?

Upvotes: 1

Views: 336

Answers (1)

erdnaxeli
erdnaxeli

Reputation: 66

It seems that executing $TEMP doesn't work correctly, as the whole string 'bash -s' -- < ${Command} is given in argument to ssh. And in fact if you create a file called ${Command} on you remote host you will get an error bash: bash -s: command not found.

A solution is to uses eval like this :

eval $TEMP

This really does what it should.

Upvotes: 1

Related Questions