bash 在远程节点上的 ssh 命令中使用 SED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40695943/
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 15:25:57  来源:igfitidea点击:

Using SED in a ssh command on a remote node

bashsshsed

提问by mahmood

I wrote a script to ssh to some nodes and run a sedcommand inside the node. The script looks like

我写了一个脚本 ssh 到一些节点并sed在节点内运行一个命令。脚本看起来像

NODES="compute-0-3"
for i in $NODES 
do
  echo $i
  ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
done

However, the error is

但是,错误是

unexpected EOF while looking for matching `''
syntax error: unexpected end of file

It seems that the character 'is not treated as the begining of a sedcommand.

似乎字符'不被视为sed命令的开始。

回答by Cyrus

I suggest to replace

我建议更换

ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'

by

经过

ssh "$i" 'sed -i "s/172.16.48.70/172.20.54.10/g" /etc/hosts'

If you absolutely want to use single quotes:

如果您绝对想使用单引号:

ssh "$i" 'sed -i '"'"'s/172.16.48.70/172.20.54.10/g'"'"' /etc/hosts'