简体   繁体   English

我的bash脚本怎么了?

[英]what's wrong with my bash script?

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

The directory structure is $path/node0/shard0 目录结构为$ path / node0 / shard0

In this script, the var shard_path is null, why? 在此脚本中,var shard_path为null,为什么? And the directory temp is constructed in the ~/ but not in the shard0 . 目录temp是在~/构造的,而不是在shard0

Because you're using double quotes around the command to send to the server, that is being expanded locally. 因为要在发送给服务器的命令周围使用双引号,所以正在本地进行扩展。 It's trying to substitute $path and $shard_path based on their local values, and since $shard_path isn't defined locally, it expands to nothing. 它试图根据$path$shard_path的本地值来替代它们,并且由于$shard_path不是本地定义的,因此扩展为$shard_path

You can avoid a lot of these quoting issues by using single quotes for the command to be executed on the server. 通过对服务器上要执行的命令使用单引号,可以避免很多此类引号问题。 The exact quoting that you need depends on exactly where these variables are defined. 您需要的确切报价取决于这些变量的确切定义位置。 But from your comments, it sounds like this is the command that you need: 但是从您的评论看来,这是您需要的命令:

ssh -f $user@$machine_name "cd $path;"'shard_path=$(find . -name "shard0"); cd $shard_path; mkdir temp'

From your discussion with Brian Campbell, I assume that the $path variable is on the local client and $shard_path is on the remote location. 通过与Brian Campbell的讨论,我假设$path变量在本地客户端上,而$shard_path在远程位置上。

Then try the following: 然后尝试以下操作:

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

Note: If there is a possibility that there are multiple files called 'shard0', find will output many paths, so you might want to use 注意:如果有可能存在多个名为“ shard0”的文件,find将输出许多路径,因此您可能需要使用
shard_path_array=( \\$(find . -name \\"shard0\\") );
This puts find's output into an array, and you can then switch to one path using for example 这会将find的输出放入一个数组中,然后您可以使用例如
cd \\${shard_path_array[0]} (on the remote machine) cd \\${shard_path_array[0]} (在远程计算机上)

$path is being expanded locally before the command is sent to the destination machine via ssh. 在通过ssh将命令发送到目标计算机之前,将在本地扩展$ path。 Ditto $shard_path. 同上$ shard_path。 If you want those values interpreted/expanded remotely, you need to escape the $, eg \\$path. 如果要远程解释/扩展这些值,则需要转义$,例如\\ $ path。

Here's a very thorough treatment on this subject . 这是关于这个问题的非常彻底的治疗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM