unix - use of ssh variable in the shell script -
i want use variables of ssh in shell script. suppose have variable value got inside ssh , want use variable outside ssh in shell itself, how can ?
ssh my_pc2 <<eof <.. operations ..> a=$(ls -lrt | wc -l) echo \$a eof echo $a in above example first echo print 10 inside ssh prints 10 second echo $a prints nothing.
i refine last answer defining special syntax passing required settings back, e.g. "#set var=value"
we put commands (that want run within ssh session) in cmdfile file this:
a=`id` b=`pwd` echo "#set a='$a'" echo "#set b='$b'" and main script this:
#!/bin/bash # ssh, run remote commands, , filter passed ssh user@host <cmdfile | grep "^#set " | sed 's/#set //' >vars.$$ # source variable settings passed . vars.$$ rm -f vars.$$ # have variables set echo "a = $a" echo "b = $b" if you're doing lots of variables, can add function cmdfile, simplify/encapsulate special syntax passing data back:
passvar() { var=$1 val=$2 val=${val:-${!var}} echo "#set ${var}='${val}'" } a=`id` passvar b=`pwd` passvar b you might need play quotes when values include whitespace.
Comments
Post a Comment