Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to catch an error exit code in node.js from shell script?

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

like image 424
axel Avatar asked Oct 28 '22 15:10

axel


1 Answers

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
like image 169
ZEE Avatar answered Oct 31 '22 08:10

ZEE