Reputation: 356
I need to perform some operations on my remote server (targeted via ssh), and use the result on my local server. I know how to perform an operation locally and use it remotely but I've not found any way of doing the reverse operation.
for example:
ssh my_remote_server '
echo "==> ${PWD}"
ERROR=3
echo "==> ${ERROR}"
MYERROR=$((ERROR + 1)) or 'MYERROR'=$((ERROR + 1))
echo "===> ${MYERROR}"
'
echo "### ${MYERROR}"
result obtained:
==> /home/toto
==> 3
===> 4
###
Expected result:
==> /home/toto
==> 3
===> 4
### 4
Does anyone know how I could use MYERROR locally?
Upvotes: 0
Views: 613
Reputation: 780798
Get rid of all the other echoing in the script that you send, and just have it echo the new value. Then you can assign the output of ssh
to a local variable.
myerror=$(ssh -T my_remote_server <<'EOF'
error=3
myerror=$((error + 1))
echo "$myerror"
EOF
)
echo "### $myerror"
If you want multiple variables, you could have the script echo variable assignments, and then use eval
eval "$(ssh -T my_remote_server <<'EOF'
echo 'echo "==> ${PWD}"'
error=6
echo 'echo "==> ${error}"'
myerror=$((error + 1))
echo 'echo "===> ${myerror}"'
echo "myerror='$myerror'"
x=2
y=$((x * 3))
echo "y='$y'"
EOF
)"
echo "### $myerror"
echo "### $y"
BTW, get out of the habit of using all-uppercase variables. By convention these names are reserved for environment variables.
Upvotes: 3