bash 如何通过 ssh 将 echo 的输出重定向到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14673316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:26:16  来源:igfitidea点击:

how to redirect output of echo over ssh to a file

linuxbashssh

提问by Hamid Reza Moradi

I'm trying to redirect content of a variable to a file through ssh. like:

我正在尝试通过 ssh 将变量的内容重定向到文件。喜欢:

ssh $MachineIP  echo $CM_Config > $mName/CM_CONFIG

where $CM_Configis a local variable in my host containing multiple line, and $mName/CM_CONFIGis located in $MachineIPhow should I redirect local variable to the remote file assuming my ssh configurations are correct. Thanks in advance

$CM_Config我的主机中包含多行的本地变量在哪里,并且$mName/CM_CONFIG位于$MachineIP假设我的 ssh 配置正确的情况下我应该如何将本地变量重定向到远程文件。提前致谢

回答by Olaf Dietsche

You must quote the whole command with double quotes

您必须用双引号引用整个命令

ssh $MachineIP "echo $CM_Config > $mName/CM_CONFIG"

This allows the variables to be replaced by the local shell and the redirection done at the remote host.

这允许变量被本地 shell 替换并在远程主机上完成重定向。

回答by Hamid Reza Moradi

In my case the problem solved with this command:

在我的情况下,此命令解决了问题:

ssh $MachineIP " echo \"$CM_Config\" > \"$mName/CM_CONFIG\" "

In fact without \" enclosing my variable, my problem didn't solved. Maybe it is because that the content of these variables are somehow like bash command and are in multiple lines.

事实上,如果没有 \" 包围我的变量,我的问题并没有解决。也许是因为这些变量的内容有点像 bash 命令,并且是多行的。