下面,我不工作:
ssh user@remote.server "k=5; echo $k;"
它只是返回一个空行。
我怎样才能在指定的远程会话(SSH)的变量?
注:我的问题不是如何局部变量传递给我的SSH会话,而是如何创建和分配远程变量。 (应该是一个非常简单的任务是什么?)
编辑:
更具体地说,我试图做到这一点:
bkp=/some/path/to/backups
ssh user@remote.server "bkps=( $(find $bkp/* -type d | sort) );
echo 'number of backups: '${#bkps[@]};
while [ ${#bkps[@]} -gt 5 ]; do
echo ${bkps[${#bkps[@]}-1]};
#rm -rf $bkps[${#bkps[@]}-1];
unset bkps[${#bkps[@]}-1];
done;"
find命令工作正常,但由于某种原因$bkps
没有得到填充。 所以我的猜测是,这将是一个变量赋值的问题,因为我觉得我已经检查了一切...
Given this invocation:
ssh user@remote.server "k=5; echo $k;"
the local shell is expanding $k
(which most likely isn't set) before it is executing ssh ...
. So the command that actually gets passed to the remote shell once the connection is made is k=5; echo ;
(or k=5; echo something_else_entirely;
if k
is actually set locally).
To avoid this, escape the dollar sign like this:
ssh user@remote.server "k=5; echo \$k;"
Alternatively, use single quotes instead of double quotes to prevent the local expansion. However, while that would work on this simple example, you may actually want local expansion of some variables in the command that gets sent to the remote side, so the backslash-escaping is probably the better route.
For future reference, you can also type set -x
in your shell to echo the actual commands that are being executed as a help for troubleshooting.