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?

marto
  • 4,402
  • 25
  • 15
shipshape
  • 691
  • 1
  • 6
  • 5

1 Answers1

76

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
Susam Pal
  • 32,765
  • 12
  • 81
  • 103
  • sorry I should have mentioned I was using cygwin. Are you sure this would convert a Windows ERRORLEVEL into the bash equivalent $? – shipshape Jul 26 '11 at 10:42
  • 9
    Yes! Why don't you simply try it out and confirm for yourself? – Susam Pal Jul 26 '11 at 16:05
  • 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 errorlevel – shipshape Jul 27 '11 at 13:20