ssh $targetHost “[[ -z $(grep..." - 失败。为什么?

ssh $targetHost “[[ -z $(grep..." - 失败。为什么?

[编辑] 根据评论#1:更多代码,更少描述

为什么这没有按预期工作?即:我想要:

  • 循环中通过 ssh 连接到一组远程主机(因此 </dev/null)
  • 检查(远程)rc.local 中是否存在设置(使用 grep)
  • 如果没有则添加它(使用 sed)

失败文件

#!/bin/bash
targetHostList="
pizerocam0
pizerocam1
pizerocam2
pizerocam3
"
while IFS= read -r targetHost;do
    [[ -z $targetHost ]] && continue
    ssh -q $targetHost "[[ -f /etc/rc.local && -z $(grep -o '/usr/bin/tvservice -o' /etc/rc.local) ]] && sudo sed -Ei 's/exit\ 0/\/usr\/bin\/tvservice\ \-o \#\ disable HDMI\n\nexit\ 0/' /etc/rc.local"
done<<<"$targetHostList"

答案1

[已解决] 谢谢 Kamil!我只是想看看。我立刻就知道这就是我想要的。我确定我以前做过这个,只是忘了怎么做。

已解决.sh

#!/bin/bash
targetHostList="
pizerocam0
pizerocam1
pizerocam2
pizerocam3
"
while IFS= read -r targetHost;do
    [[ -z $targetHost ]] && continue

ssh -q $targetHost bash <<'EOF'
[[ -f /etc/rc.local && -z $(grep -o '/usr/bin/tvservice -o' /etc/rc.local) ]] && sudo sed -Ei 's/exit\ 0/\/usr\/bin\/tvservice\ \-o \#\ disable HDMI\n\nexit\ 0/' /etc/rc.local
EOF

done<<<"$targetHostList"

相关内容