简体   繁体   English

远程命令替换失败

[英]Remote command substitution fails

What is missing here?这里缺少什么?

This works (proves the file is there):这有效(证明文件在那里):

[rundeck@den16 ~]$ ssh ${SSH_USER}@${BUILD_HOST} "cat ${WLS_E1DOMAIN_LOC}/nodemanager.process.id"
7504

This doesn't (says No such file or directory):这不会(说没有这样的文件或目录):

[rundeck@den16 ~]$ ssh ${SSH_USER}@${BUILD_HOST} "kill -9 `cat ${WLS_E1DOMAIN_LOC}/nodemanager.process.id`"
cat: /cygdrive/c/Oracle/Middleware/Oracle_Home/user_projects/domains/E1DevDomain/nodemanager/nodemanager.process.id: No such file or directory

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

As @kvantour said, the problem is that the command expansion is happening on the local computer.正如@kvantour 所说,问题是命令扩展发生在本地计算机上。 Another way to solve this is to use escapes to tell the local shell to pass through the characters that normally trigger command expansion:解决此问题的另一种方法是使用转义符告诉本地 shell 传递通常会触发命令扩展的字符:

ssh ${SSH_USER}@${BUILD_HOST} "kill -9 \`cat ${WLS_E1DOMAIN_LOC}/nodemanager.process.id\`"
                                       ^                                               ^

BTW, I also recommend using $( ) instead of backticks (and you only have to escape the $ , not the parentheses):顺便说一句,我还建议使用$( )而不是反引号(并且您只需转义$ ,而不是括号):

ssh ${SSH_USER}@${BUILD_HOST} "kill -9 \$(cat ${WLS_E1DOMAIN_LOC}/nodemanager.process.id)"
                                       ^

BTW^2, you can use echo on the command part of the ssh command to see what'll be sent to the remote computer.顺便说一句,您可以在ssh命令的命令部分使用echo来查看将发送到远程计算机的内容。 That is, it shows the command after the local shell has done its parsing, substitutions, etc, but before the remote shell has done its parsing etc. Like this:也就是说,它在本地 shell 完成解析、替换等之后,但在远程 shell 完成解析等之前显示命令。像这样:

$ WLS_E1DOMAIN_LOC=/some/path
$ echo "kill -9 \$(cat ${WLS_E1DOMAIN_LOC}/nodemanager.process.id)"
kill -9 $(cat /some/path/nodemanager.process.id)

Here, you can see that $WLS_E1DOMAIN_LOC did get substituted by the local shell, but the command substitution didn't.在这里,您可以看到$WLS_E1DOMAIN_LOC确实被本地 shell 替换了,但命令替换没有。

(This is one of very few cases where echo ing a command is informative, rather than misleading.) (这是为数不多的echo命令提供信息而不是误导的情况之一。)

You are trying to pass a kill command to a server over ssh.您正在尝试通过 ssh 将 kill 命令传递给服务器。

Unfortunately, all substitutions are done on the host side, and not the server side.不幸的是,所有替换都是在主机端完成的,而不是服务器端。 The error you are getting from cat is an error which is generated on den16 and not BUILD_HOST .您从cat得到的错误是在den16而不是BUILD_HOST上生成的错误。 If you want to pass it to BUILD_HOST you have to use a pipe in this case.如果要将其传递给BUILD_HOST ,则在这种情况下必须使用管道。 Normally you would use single quotes, but since you use shell variables already in there, you have to use a pipe通常你会使用单引号,但由于你已经在那里使用了 shell 变量,你必须使用管道

rundeck@den16 ~]$ ssh ${SSH_USER}@${BUILD_HOST} "cat ${WLS_E1DOMAIN_LOC}/nodemanager.process.id | xargs kill -9"

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

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