简体   繁体   English

Bash脚本从NodeJs脚本返回退出代码

[英]Bash script to return exit code from NodeJs script

I have a Bash script which runs a node script as part of its tasks. 我有一个Bash脚本,它运行节点脚本作为其任务的一部分。 I would like the bash script to exit with the same exit code as the node script. 我希望bash脚本以与节点脚本相同的退出代码退出。 Simplified example below. 以下是简化示例。

foo.sh: foo.sh:

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

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 来自: 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. 这意味着您只需要在上面的脚本中添加“exit”。

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

Bash stores the exit code of the previous command in a variable named $? Bash将前一个命令的退出代码存储在名为$?的变量中$?

#!/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 获得退出代码后,您可以决定要做什么,包括退出,如其他答案中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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