Bash script to return exit code from NodeJs script

I have a Bash script that executes a node script as part of its tasks. I would like the Bash script to exit with the same exit code as the node script. A simplified example is below.

foo.sh:

#!/bin/bash
node ./bar.js

      

bar.js:

process.exit(1); //sometimes the exit code can be 0

      

+3


source to share


2 answers


From: http://www.tldp.org/LDP/abs/html/exit-status.html

When the script ends with an exit that has no parameters, the exit status of the script is the exit status of the last command executed in the script



which means you just need to add "exit" to your above script.

#!/bin/bash
node ./bar.js
exit

      

+8


source


Bash stores the exit code of the previous command in a variable named $?

#!/bin/bash
node ./bar.js
# will print nodes exit code, e.g. 0 for success
echo $?

      



EDIT

Once you have the exit code, you can decide what to do, including the exit, as mentioned in another answer

+2


source







All Articles