0
ssh -f $user@$machine_name "cd $path; shard_path=`find . -name \"shard0\"`; cd $shard_path; mkdir temp"

目录结构为 $path/node0/shard0

在这个脚本中,varshard_path为空,为什么?并且该目录temp是在 中构造的,~/而不是在shard0.

4

3 回答 3

2

因为您在命令周围使用双引号发送到服务器,所以正在本地扩展。它试图替换$path$shard_path基于它们的本地值,并且由于$shard_path没有在本地定义,它扩展为空。

您可以通过对要在服务器上执行的命令使用单引号来避免很多这些引用问题。您需要的确切引用取决于这些变量的确切定义位置。但从您的评论来看,这听起来像是您需要的命令:

ssh -f $user@$machine_name "cd $path;"'shard_path=$(find . -name "shard0"); cd $shard_path; mkdir temp'
于 2012-12-05T06:59:53.023 回答
1

根据您与 Brian Campbell 的讨论,我假设该$path变量位于本地客户端并且$shard_path位于远程位置。

然后尝试以下操作:

ssh -f xce@ns20.xce.n.xiaonei.com "echo $path; cd $path; shard_path=\$(find . -name \"shard0\"); echo \$shard_path;"

注意:如果有可能有多个名为 'shard0' 的文件,find 会输出很多路径,所以你可能想使用
shard_path_array=( \$(find . -name \"shard0\") );
This将find的输出放入一个数组中,然后你可以使用例如切换到一个路径
cd \${shard_path_array[0]}(在远程机器)

于 2012-12-05T10:50:15.620 回答
0

$path 在命令通过 ssh 发送到目标机器之前在本地扩展。同上 $shard_path。如果您希望远程解释/扩展这些值,您需要转义 $,例如 \$path。

这是关于这个主题的非常彻底的处理

于 2012-12-05T06:57:13.887 回答