简体   繁体   中英

Can and how do you access the last bash exit code from a NodeJS app running in bash

So the scenario I have here is that I want to be able to run a set of bash commands like so

command; nodeapp

I want to run the command and then have the nodeapp read the exitcode of the command run before the nodeapp to know if it failed or was successful. How do I do this?

I've looked at $? and $PIPESTATUS which is what would normally work for a bash script but it appears those environment variables do not get passed to the node application via process.env .

How could I accomplish this?

I know I could just make my nodeapp run a childprocess with the command but it just feels a bit unintuitive to me.

I don't know how do you want the nodeapp to read the exit code, but in any case you may put the exit code in a variable and then use it however you want in bash.

command1;code=$?;....

Maybe using && and || can help for your solution. Examples:

command1 && command2 #command2 runs only if command1 is successful.

command1 || command2 command1 || command2 #command2 run if command1 is not successful.

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