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"
2
  • 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 </dev/null for ssh or for sudo? In general: what is expected? Jan 4 at 13:44
  • See this and this. Jan 4 at 13:46
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.