accessing ERRORLEVEL from bash script

BashCmdErrorlevel

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshipshapeView Question on Stackoverflow
Solution 1 - BashSusam PalView Answer on Stackoverflow