accessing ERRORLEVEL from bash script

shipshape picture shipshape · Jul 26, 2011 · Viewed 49.8k times · Source

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?

Answer

Susam Pal picture Susam Pal · Jul 26, 2011

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