0

[edit] per comment #1: more code, less description

Why doesn't this work as expected? ie: i want to :

  • ssh to a bunch of remote hosts in a loop (hence the </dev/null)
  • check if a setting is present in (the remote) rc.local (with grep)
  • if not then add it (with sed)

fail.sh

#!/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"
BETLOG
  • 21
  • 3
  • What is expected? `$(grep …)` gets expanded *locally*, apparently it returns an empty string. Depending on where you want to expand it, you should double-quote or single-quote (or escape). After the local shell removes quotes it does interpret, the resulting command string will be interpreted *again* on the remote side. At this point it should contain quotes to be interpreted *there*. So you need at least two levels of quoting/escaping. For now I'm not sure if you want `$()` to work locally. How about `&&`? Is ` – Kamil Maciorowski Jan 04 '21 at 13:44
  • See [this](https://superuser.com/a/1534401/432690) and [this](https://unix.stackexchange.com/a/626511/108618). – Kamil Maciorowski Jan 04 '21 at 13:46

1 Answers1

1

[SOLVED] Thanks Kamil! I just needed to see it. Knew it was what I wanted immediately. I'm sure i've done this before and just forgotten how.

SOLVED.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"
BETLOG
  • 21
  • 3