Linux Command Line Error Codes
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 bash exit code check parent process.Every command returns an exit status (sometimes referred toLinux Exit Code
as a return status or exit code). A successful command returns a 0, while an bash exit codes unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are
Exit Code 0
some exceptions.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or 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.Resources Polls Contact Me / Advertising Search This Blog Monday, March 24, 2008 How to check the exit status code When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the
Exit Code 1
exit code, you need to examine a special variable, "$?" Say, you are searching for a
Bash Script Exit On Error
string in a text file. $ grep x1y2z3 somefile.txt $ The standard output of the command returns null, which is a pretty good indication that shell script exit the string cannot be found in the file. But what if you embed the grep command in a script? How can you tell if the string is found or not? Checking the exit code will tell you. Let's first try it http://tldp.org/LDP/abs/html/exit-status.html out interactively. $ grep x1y2z3 somefile.txt $ echo $? 1 Note that in bash, the exit status is 0 if the command succeeded, and 1 if failed. For grep, 0 means that the string was found, and 1 (or higher), otherwise. To check the exit status in a script, you may use the following pattern: somecommand argument1 argument2 RETVAL=$? [ $RETVAL -eq 0 ] && echo Success [ $RETVAL -ne 0 ] && echo Failure Posted by Peter Leung at 7:50 PM http://linuxcommando.blogspot.com/2008/03/how-to-check-exit-status-code.html Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest 4 comments: Mad MAx said... $RETVAL=$?is not correct.when you assign a value to a variable, in bash, you can't prepend variable name with $.The correct form isRETVAL=$?regards, Mad Max. August 4, 2009 at 3:07 AM Peter Leung said... Thanks, Mad MAx.I made the correction.Peter August 4, 2009 at 8:50 PM Anonymous said... You can also just avoid the RETVAL altogether and use the "||" or "&&" operands which are called when the command on the left returns 1 or 0 respectively, e.g.# grep returns 1, e.g. no matchgrep foo /tmp/bar.txt || echo "text not found"# grep returns 0, e.g. matchgrep baz /tmp/bar.txt && echo "found it!" April 25, 2011 at 2:56 PM Wang said... Thanks for sharing, and thanks for the above comment. July 31, 2012 at 11:59 AM Post a Comment Newer Post Older Post Home Subscribe to: Post Comments (Atom) Followers Subscribe in a reader Enter your email address:Delivered by FeedBurner Follow @commandolinux Popular Posts How to disable SSH host key checking How to Display Routing Table How to connect to a WPA/WPA2 WiFi network using Linux command line Using sed to extract lines in a text file How to get the process start date and time Blog Archive ► 2016 (2) ► July (1) ► January (1) ► 2015 (20) ► November (1) ► October (1) ► July (2) ► June (2) ► May (2) ► April (5) ► March (2) ► February
exit codes, exit codes are important and this article describes how to use them in your scripts and understand them in general. Written by Benjamin Cane on 2014-09-02 14:45:00| 4 min read Sponsored by Lately I've been working on a lot http://bencane.com/2014/09/02/understanding-exit-codes-and-how-to-use-them-in-bash-scripts/ of automation and monitoring projects, a big part of these projects are taking existing scripts and http://www.thegeekstuff.com/2010/10/linux-error-codes modifying them to be useful for automation and monitoring tools. One thing I have noticed is sometimes scripts use exit codes and sometimes they don't. It seems like exit codes are easy for poeple to forget, but they are an incredibly important part of any script. Especially if that script is used for the command line. What are exit codes? On Unix and Linux systems, exit code programs can pass a value to their parent process while terminating. This value is referred to as an exit code or exit status. On POSIX systems the standard convention is for the program to pass 0 for successful executions and 1 or higher for failed executions. Why is this important? If you look at exit codes in the context of scripts written to be used for the command line the answer is very simple. Any script that is useful in some fashion bash exit code will inevitably be either used in another script, or wrapped with a bash one liner. This becomes especially true if the script is used with automation tools like SaltStack or monitoring tools like Nagios, these programs will execute scripts and check the status code to determine whether that script was successful or not. On top of those reasons, exit codes exist within your scripts even if you don't define them. By not defining proper exit codes you could be falsely reporting successful executions which can cause issues depending on what the script does. What happens if I don't specify an exit code In Linux any script run from the command line has an exit code. With Bash scripts, if the exit code is not specified in the script itself the exit code used will be the exit code of the last command run. To help explain exit codes a little better we are going to use a quick sample script. Sample Script: #!/bin/bash touch /root/test echo created file The above sample script will execute both the touch command and the echo command. When we execute this script (as a non-root user) the touch command will fail, ideally since the touch command failed we would want the exit code of the script to indicate failure with an appropriate exit code. To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of
In C programming language, there is no direct support for error handling. You have to detect the failure and handle the error. In C programming language, return values represents success or failure. Inside a C program, when a function fails, you should handle the errors accordingly, or at least record the errors in a log file. When you are running some program on Linux environment, you might notice that it gives some error number. For example, "Error no is : 17", which doesn't really say much. You really need to know what error number 17 means. This article shows all available error numbers along with it descriptions. This article might be a handy reference for you, when you encounter an error number and you would like to know what it means. In C programming language, there is an external variable called "errno". From this errno variable you can use some error handling functions to find out the error description and handle it appropriately. You have to include errno.h header file to use external variable errno. perror function prints error description in standard error. The strerror function returns a string describing the error code passed in the argument errnum. The following C code snippet tries to open a file through open system call. There are two flags in the open call. O_CREAT flag is to create a file, if the file does not exist. O_EXCL flag is used with O_CREAT, if the file is already exist open call will fail with the proper error number. $ cat fileopen.c #include