0

I am trying to run some commands on a remote server using EOF however I see some issues. Please see below

This works:

#!/bin/sh
ssh -4q <some-server> sh <<'EOF'
                        E_Distro=<somebody>@gmail.com
                        CONTROLM_FOLDER="/var/opt/bmc_ctm"
                        COUNTER=$(find $CONTROLM_FOLDER/ -type f -size +50M -exec ls -lrth {} \; | wc -l)
                        THRESHOLD=20
                        if [[ $COUNTER != 0 ]]; then
                               echo -e "something" | mail -s "Test" $E_Distro
                        fi
EOF

This does not:

ssh -4q <some-server> sh <<EOF
                        E_Distro=<somebody>@gmail.com
                        CONTROLM_FOLDER="/var/opt/bmc_ctm"
                        COUNTER=$(find $CONTROLM_FOLDER/ -type f -size +50M -exec ls -lrth {} \; | wc -l)
                        THRESHOLD=20
                        if [[ $COUNTER != 0 ]]; then
                               echo -e "something" | mail -s "Test" $E_Distro
                        fi
EOF

I get below error if I use EOF without quotes

~/scripts/mysripts> sh EOF_test.sh
find: ‘/home/admb8rwd0’: Permission denied
find: ‘/home/admb7rpb0/.ssh’: Permission denied
find: ‘/home/admf82nh0’: Permission denied
find: ‘/home/admfpvdh0’: Permission denied
find: ‘/home/admdk35b0/.ssh’: Permission denied
find: ‘/home/admtqw3m0’: Permission denied
find: ‘/home/admfk5rm0’: Permission denied
find: ‘/home/admjxwxb0’: Permission denied
find: ‘/home/admgv1pc0’: Permission denied
find: ‘/home/admfg252b’: Permission denied
find: ‘/home/admgykmq0’: Permission denied

I read online that if we use quotes for EOF token it does not expand variables however I see a reverse behavior here. Please suggest what is correct way to use EOF

bomsabado
  • 35
  • 6
  • There was a typo while posting the question. Correction below ssh -4q someserver sh <<'EOF' – bomsabado Mar 17 '21 at 19:20
  • What the correct way to use a heredoc is (`EOF` is a conventional sigil, but any other string can be used as a sigil as well) depends on how you want it to work. – Charles Duffy Mar 17 '21 at 19:23
  • 1
    The question here is whether expansions happen _before_ `ssh` is started (by the local shell), or _after_ `ssh` is started (by the remote shell). Suppressing expansions by the local shell doesn't mean the remote shell won't do expansions after it receives the code on its stdin; it just means it won't be munged before the remote system receives it. – Charles Duffy Mar 17 '21 at 19:24
  • BTW, insofar as you're tagging this `bash` instead of `sh`, you should probably be using `bash` instead of `sh` in your ssh command, if that's the shell you want to use to interpret the code. – Charles Duffy Mar 17 '21 at 19:24
  • 1
    ...when you use `< – Charles Duffy Mar 17 '21 at 19:26
  • ...similarly, `< – Charles Duffy Mar 17 '21 at 19:27
  • Thank you Charles. So, I should be using EOF with quotes for the variables to be expanded? – bomsabado Mar 17 '21 at 20:28
  • If you want all expansions to happen on the remote side, then yes, you want to quote your heredoc sigil. (Part of why I'm saying "sigil" instead of "EOF" is that `<<"foobar"` would work just as well, as long as you have a line containing only `foobar` at the end; you can use that to nest heredocs, by using different sigils from the inner and outer ones). – Charles Duffy Mar 17 '21 at 20:33
  • BTW, if you want to log what's happening during execution, consider changing `ssh ... sh` to `ssh ... sh -x`, especially if you run `set -x` on a line _before_ the ssh command (this can be reversed with `set +x` on a line after the whole thing is done). – Charles Duffy Mar 17 '21 at 20:35

0 Answers0