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

mah*_*ood 2 ssh bash sed

我写了一个脚本 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
Run Code Online (Sandbox Code Playgroud)

但是,错误是

unexpected EOF while looking for matching `''
syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

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

Cyr*_*rus 7

我建议更换

ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
Run Code Online (Sandbox Code Playgroud)

经过

ssh "$i" 'sed -i "s/172.16.48.70/172.20.54.10/g" /etc/hosts'
Run Code Online (Sandbox Code Playgroud)

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

ssh "$i" 'sed -i '"'"'s/172.16.48.70/172.20.54.10/g'"'"' /etc/hosts'
Run Code Online (Sandbox Code Playgroud)