8

as the title of the question, here is the scenario.

A shell script file script.sh that does some operations and at some point it requires to launch a node file.

#! /bin/bash

node "$(dirname "$0")/script.js" "$input"

echo "${?}\n"

In the node file script.js there are some controls and in case of error the script return with an error exit code.

process.exit(1)

Is it possible to catch this error exit code in order to let the command to be executed in the shell script script.sh?

Currently the execution is interrupted with this error error Command failed with exit code 1., as expected by the way. But I would like to know if I can on shell script to catch this error and continue to execute the last part of the code echo "${?}\n".

Thanks in advance

2
  • What was the value of echo "${?}" printed? Commented Apr 24, 2019 at 7:58
  • As I said, the execution is interrupted so nothing happens in script.sh after calling node "$(dirname "$0")/script.js" "$input" . But I would expect echo to print 1. Commented Apr 24, 2019 at 8:00

1 Answer 1

4

You can do something like this in your bash script in case your node script return 1

node "$(dirname "$0")/script.js" "$input"
   echo "Script: $? - Successfull"
if [ $? != 0 ]; then                   
   echo "${?}\n". 1>&2 && exit 1
fi
Sign up to request clarification or add additional context in comments.

2 Comments

I've just on my standard output this error => error Command failed with exit code 1. and anything else happens in the script.sh after calling node "$(dirname "$0")/script.js" "$input".
ah, it is because of set -o errexit coming from another shell script that actually calls mine.

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.