简体   繁体   中英

Bash script to return exit code from NodeJs script

I have a Bash script which runs 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. Simplified example below.

foo.sh:

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

bar.js:

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

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

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

which implies that you simply need to add "exit" to your above script.

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

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 exiting as mentioned in the other answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM