Get Error Code Bash
Contents |
Ramey
The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent process.Every command returns an exit status (sometimes referred bash if exit code to as a return status or exit code). A successful commandBash Script Exit On Error
returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands,
Bash Return Value From Function
programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the function orExit Bash Shell
script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).
When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the exit).#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exitThe equivalent of a bare exit is exit $? or even just omitting the exit.#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exit $?#!/bin/bash COMMAND1 . . . COMMAND_LAST # Will exit with status of last command.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 a poor one is often measured in terms of exit code 0 the program's robustness. That is, the program's ability to handle situations in which something goes shell script exit wrong. Exit status As you recall from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes bash exit function successfully, the exit status will be zero. If the exit status is anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. http://tldp.org/LDP/abs/html/exit-status.html 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 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 http://linuxcommand.org/wss0150.php 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 status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * el
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/5195607/checking-bash-exit-status-of-several-commands-efficiently and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation http://askubuntu.com/questions/29370/how-to-check-if-a-command-succeeded Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it exit code only takes a minute: Sign up Checking Bash exit status of several commands efficiently up vote 165 down vote favorite 71 Is there something similar to pipefail for multiple commands, like a 'try' statement but within bash. I would like to do something like this: echo "trying stuff" try { command1 command2 command3 } And at any point, if any command get error code fails, drop out and echo out the error of that command. I don't want to have to do something like: command1 if [ $? -ne 0 ]; then echo "command1 borked it" fi command2 if [ $? -ne 0 ]; then echo "command2 borked it" fi And so on... or anything like: pipefail -o command1 "arg1" "arg2" | command2 "arg1" "arg2" | command3 Because the arguments of each command I believe (correct me if I'm wrong) will interfere with each other. These two methods seem horribly long-winded and nasty to me so I'm here appealing for a more efficient method. bash exit share|improve this question edited Nov 28 '14 at 6:39 Andy Shulman 84211027 asked Mar 4 '11 at 15:19 jwbensley 2,475103764 add a comment| 11 Answers 11 active oldest votes up vote 185 down vote accepted You can write a function that launches and tests the command for you. Assume command1 and command2 are environment variables that have been set to a command. function mytest { "$@" local status=$? if [ $status -ne 0 ]; then echo "
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to check if a command succeeded? up vote 63 down vote favorite 22 Is there any way to check if there is an error in executing a command? Example : test1=`sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase" $search` valid $test1 function valid () { if $test -eq 1; then echo "OK" else echo "ERROR" fi } I already tried do that but it seems it isn't working. I don't how do that. command-line share|improve this question edited Mar 7 '11 at 13:41 Octavian Damiean 10.2k54260 asked Mar 7 '11 at 11:40 moata_u 72861523 5 Prefer $(foo) over backticks `foo`, because you can nest it, and it's easier to distinguish from apostrophes. –user unknown Mar 26 '11 at 18:19 add a comment| 5 Answers 5 active oldest votes up vote 100 down vote accepted The return value is stored in $?. 0 indicates success, others indicates error. some_command if [ $? -eq 0 ]; then echo OK else echo FAIL fi Like any other textual value, you can store it in a variable for future comparison: some_command retval=$? do_something $retval if [ $retval -ne 0 ]; then echo "Return code was not zero but $retval" fi For possible comparison operators, see man test. share|improve this answer edited Mar 7 '11 at 13:14 answered Mar 7 '11 at 12:06 Lekensteyn 86.1k35220293 Th