1

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?

14
  • Is the script finishing, or is it hanging and never returning? Commented Jun 24, 2021 at 13:49
  • Script finishing with error code 1 Commented Jun 24, 2021 at 13:51
  • 1
    Try it with ssh -vvv for verbose logging, and ssh -F /dev/null to rule out any configuration file issues. Commented Jun 24, 2021 at 14:53
  • 1
    Investigate the machine being SSHed to. There's something fishy there. Check out its SSH config, the remote .bashrc and /etc/profile, etc. Commented Jun 24, 2021 at 15:18
  • 1
    If you add echo "$SHELLOPTS" before you call ssh, what does it report? Commented Jun 24, 2021 at 19:14

2 Answers 2

1

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"

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.