Polkovski
Polkovski

Reputation: 23

Bash script stop on exit in ssh

I have a fragment in a bash script:

ssh -oStrictHostKeyChecking=no -T $SSH_HOSTNAME -p $SSH_PORT <<EOM
    set -e
    echo "test"
    exit
EOM

echo "test 2"

Statement "test" is displayed but "test 2" is not. I also tried to use "exit 0" instead of "exit".

The script worked on an older version of debian (2016), but stopped working after the update (2021). Why the script stopped working and how can I fix it?

Upvotes: 1

Views: 826

Answers (2)

Alberto P.
Alberto P.

Reputation: 88

I tested that and, indeed, sending the command with heredoc and this combination of set -e and exit returns a 1 and not a 0, as tested. But I think that, somewhere, another set -e is set in the context where echo "test2" is executed. We have some solutions that would work:

  1. Remove the exit because it's not needed, you're sending commands to a target ssh host and the termination of session it's automatic when the heredocs finish.
  2. If the script is not so long, switch to inline commands ssh target "set -e ; echo \"test\"; exit" works just fine(somehow), but you have to take care of string escaping.

Upvotes: 0

lollipopman
lollipopman

Reputation: 11

Since this is only a fragment, do you have set -e enabled before the fragment in the script? If so, then I assume ssh is returning a non-zero exit code and due to set -e being enabled the script exits before reaching echo "test 2"

Upvotes: 1

Related Questions