0

I have a shell script with below ssh command to perform few actions,

$SSH $user@$remoteIpAddress "sudo rm -rf $remoteLocation/xxx/*; cd $remoteLocation/yyy; .... ... "

Directory $remoteLocation/yyy is not available and it will display the error as,

./test.sh: line 6: cd: /opt/test/yyy: No such file or directory

and it is proceeding to the next line. My scenario is to exit from the shell script itself if any command in ssh returns non-zero value. I could add set -e to exit from shell but i am not sure how to handle it in ssh. Thanks in advance.

EDIT :

I have few lines of command below the SSH command. I need a solution to exit entirely from the script and not to execute any lines below the ssh command.

1 Answer 1

1

Keep in mind that your set -e idea applies just as well to the shell you open on the other end with ssh. So you can prepend your ssh commands with that to ensure an error terminates the ssh session:

$SSH $user@$remoteIpAddress "set -e; sudo rm -rf $remoteLocation/xxx/*; cd $remoteLocation/yyy; .... ... "

Then the ssh shell will exit with non-zero error code as soon as an error is encountered, and your script will exit as well if you have set -e earlier in your script.

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

1 Comment

Thanks @spinup. Seems to be easy way and works perfectly

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.