Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing ERRORLEVEL from bash script

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?

like image 217
shipshape Avatar asked Jul 26 '11 09:07

shipshape


People also ask

How do you check exit status in bash?

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.

How do I find exit code from previous 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.

What is the command to check exit status in Linux?

“$?” 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.

How does a function return a value in bash?

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.


1 Answers

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 
like image 186
Susam Pal Avatar answered Oct 08 '22 20:10

Susam Pal