I'm need to get the value of a variable from a remote host in a script using SSH and have to source an environment file first that does a shift at the end of its run.

The command works locally and I can see what it's doing with an echo. I've spent a good day researching this and testing every combination of single and double quotes I could find. I hope someone has a quick solution.

The first SSH command returns "YES" which is what I expect. How do I get the second variable to the remote host without it being expanded?

Here's the script:

set -x
ssh -q $SIDHost ". /tmp/useq $ThisSID; echo \$HOST_IS_ERPDB"
ERPTest=`ssh -q ${SIDHost} ". /usr/vmmc/bin/oracle_scripts/useq $ThisSID; echo \$HOST_IS_ERPDB"`

Script run:

+ SIDHost=p16
+ ThisSID=test
+ ssh -q p16 . /usr/vmmc/bin/oracle_scripts/useq test; echo $HOST_IS_ERPDB
+ + ssh -q p16 . /usr/vmmc/bin/oracle_scripts/useq test; echo NO

Inside the backticks, a backslash quotes the next character. So you're getting the output from the command

ssh -q ${SIDHost} ". /usr/vmmc/bin/oracle_scripts/useq $ThisSID; echo $HOST_IS_ERPDB"

You need \\ instead:

ERPTest=`ssh -q ${SIDHost} ". /usr/vmmc/bin/oracle_scripts/useq $ThisSID; echo \\$HOST_IS_ERPDB"`

so that ERPTest is set to the output of the command

ssh -q ${SIDHost} ". /usr/vmmc/bin/oracle_scripts/useq $ThisSID; echo \$HOST_IS_ERPDB"

Alternatively, use $(…) instead of the obsolescent `…`. Note that on some versions of AIX you may need to use /bin/sh or ksh93 instead of the antique /bin/ksh.

ERPTest=$(ssh -q ${SIDHost} ". /usr/vmmc/bin/oracle_scripts/useq $ThisSID; echo \$HOST_IS_ERPDB")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.