check error code in linux
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 linux error codes 127 an exit code. The exit code is not displayed on the screen by
Linux Kernel Error Codesdefault. To examine the exit code, you need to examine a special variable, "$?" Say, you are searching for
Linux Exit Codesa string in a text file. $ grep x1y2z3 somefile.txt $ The standard output of the command returns null, which is a pretty good indication that the string cannot be found in
Linux Errorcodethe 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 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 errno linux 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 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
RameyThe exit command terminates a script, just as in a C program. It can also return a value, which is available to the windows error codes script's parent process.Every command returns an exit status (sometimes referred ubuntu error codes to as a return status or exit code). A successful command returns a 0, while unix error codes an 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 http://linuxcommando.blogspot.com/2008/03/how-to-check-exit-status-code.html are 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 http://tldp.org/LDP/abs/html/exit-status.html - 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.
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 http://bencane.com/2014/09/02/understanding-exit-codes-and-how-to-use-them-in-bash-scripts/ 2014-09-02 14:45:00| 4 min read Sponsored by Lately I've been working on a lot https://bash.cyberciti.biz/guide/The_exit_status_of_a_command of automation and monitoring projects, a big part of these projects are taking existing scripts and 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 error code 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, 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 check error code 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 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
or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sys admin. Contents 1 Exit Status 2 How Do I See Exit Status Of The Command? 2.1 How Do I Store Exit Status Of The Command In a Shell Variable? 2.1.1 Exit Status Shell Script Example Exit Status Every Linux command executed by the shell script or user, has an exit status. The exit status is an integer number. The Linux man pages stats the exit statuses of each command. 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was failure. You can use special shell variable called? to get the exit status of the previously executed command. To print? variable use the echo command: echo $? date # run date command echo $? # print exit status foobar123 # not a valid command echo $? # print exit status How Do I See Exit Status Of The Command? Type the following command: date To view exist status of date command, enter: echo $? Sample Output: 0 Try non-existence command date1 echo $? ls /eeteec echo $? Sample Output: 2 According to ls man page - exit status is 0 if OK, 1 if minor problems, 2 if serious trouble. How Do I Store Exit Status Of The Command In a Shell Variable? Assign $? to a shell variable: ls -l /tmp status=$? echo "ls command exit stats - $status" Exit Status Shell Script Example A simple shell script to locate username (finduser.sh) #!/bin/bash # set var PASSWD_FILE=/etc/passwd # get user name read -p "Enter a user name : " username # try to locate username in in /etc/passwd grep "^$username" $PASSWD_FILE > /dev/null # store exit status of grep # if found grep will return 0 exit stauts # if not found, grep will return a nonzero exit stauts status=$? if test $status -eq 0 then echo "User '$username' found in $PASSWD_FILE file." else echo "User '$username' not found in $PASSWD_FILE file." fi Save and close the file. Run it as follows: chmod +x finduser.sh ./finduser.sh Sample Outputs: Enter a user name: vivek User 'vivek' found in /etc/passwd file. Run it again: chmod +x finduser.sh ./finduser.sh Sample Outputs: Enter a user name: tommy User 'tommy' not found in /etc/passwd file. You can combine the grep and if command in a single statement as follows: if grep "^$username:" /etc/passwd >/dev/null then echo "User '$username' found in $PASSWD_FILE file." else echo "User '$username' not found in $PASSWD_FILE file." fi Notice that standar