I have an application that only works properly when called from a windows command prompt. Something to do with the input/output streams.
So I can call it from a bash script by passing it as an argument to cmd.
cmd /c "badapp"
This works fine - but occasionally badapp fails with network problems - and I get no feedback. Is there anyway to check the ERRORLEVEl from the bash script - or see the output from badapp on the terminal running the bash script?
To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command.
Extracting the elusive exit code To display the exit code for the last command you ran on the command line, use the following command: $ echo $? The displayed response contains no pomp or circumstance. It's simply a number.
“$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred. The bash sets “$?” To the exit status of the last executed process.
Bash function can return a string value by using a global variable. In the following example, a global variable, 'retval' is used. A string value is assigned and printed in this global variable before and after calling the function. The value of the global variable will be changed after calling the function.
Yes, $?
is the variable that contains the error level.
Try echo $?
for example.
An example from Cygwin bash (I'm guessing you are using Cygwin because you are using the Windows cmd
in your example.)
susam@nifty /cygdrive/c/Documents and Settings/susam/Desktop $ cmd /c "badapp" 'badapp' is not recognized as an internal or external command, operable program or batch file. susam@nifty/cygdrive/c/Documents and Settings/susam/Desktop $ if [ $? -eq 0 ] > then > echo "good" > else > echo "bad" > fi bad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With