Linux Script Error Level
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more shell script exit code about Stack Overflow the company Business Learn more about hiring developers or posting ads
Bash If Exit Code
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow exit bash shell is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up accessing ERRORLEVEL from bash script up vote 19 down vote favorite 4 I bash script exit on error 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
Shell Script Error Handling
- or see the output from badapp on the terminal running the bash script? bash cmd errorlevel share|improve this question edited Jul 26 '11 at 9:38 marto 3,0741314 asked Jul 26 '11 at 9:17 shipshape 121125 add a comment| 1 Answer 1 active oldest votes up vote 24 down vote 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 share|improve this answer answered Jul 26 '11 at 9:39 Susam Pal 10.8k24670 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 4 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. T
and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between a good program and linux exit code a poor one is often measured in terms of the program's robustness. That is, linux exit codes the program's ability to handle situations in which something goes wrong. Exit status As you recall from previous lessons, every
Bash Catch Error
well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero. If the exit status is anything other than zero, then the program failed in http://stackoverflow.com/questions/6827837/accessing-errorlevel-from-bash-script some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return a meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: # Example of a really bad idea cd $some_directory rm * Why is http://linuxcommand.org/wss0150.php this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what happens if the directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit statu
Bash Prompts About Writing Robust Bash Shell Scripts Many people hack together shell scripts quickly to do simple tasks, but these soon take on a life of their own. Unfortunately shell scripts are full of subtle effects which http://www.davidpashley.com/articles/writing-robust-shell-scripts/ result in scripts failing in unusual ways. It's possible to write scripts which minimise these problems. In this article, I explain several techniques for writing robust bash scripts. Use set -u How often have you written a script that broke because a variable wasn't set? I know I have, many times. chroot=$1 ... rm -rf $chroot/usr/share/doc If you ran the script above and accidentally forgot to give a exit code parameter, you would have just deleted all of your system documentation rather than making a smaller chroot. So what can you do about it? Fortunately bash provides you with set -u, which will exit your script if you try to use an uninitialised variable. You can also use the slightly more readable set -o nounset. david% bash /tmp/shrink-chroot.sh $chroot= david% bash -u /tmp/shrink-chroot.sh /tmp/shrink-chroot.sh: line 3: $1: unbound variable david% linux exit code Use set -e Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit. Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use: command if [ "$?"-ne 0]; then echo "command failed"; exit 1; fi could be replaced with command || { echo "command failed"; exit 1; } or if ! command; then echo "command failed"; exit 1; fi What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly. set +e command1 command2 set -e On a slightly related note, by