accessing ERRORLEVEL from bash script
Asked Answered
B

1

54

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?

Bullshit answered 26/7, 2011 at 9:17 Comment(0)
G
77

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
Gadroon answered 26/7, 2011 at 9:39 Comment(3)
sorry I should have mentioned I was using cygwin. Are you sure this would convert a Windows ERRORLEVEL into the bash equivalent $?Bullshit
Yes! Why don't you simply try it out and confirm for yourself?Gadroon
thanks this does seem to work in general. The problem seems to be with "badapp" -when it fails with network problems it doesn't consistently set the errorlevelBullshit

© 2022 - 2024 — McMap. All rights reserved.