Linux Shell Error Code
Contents |
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 bash if exit code Cane on 2014-09-02 14:45:00| 4 min read Sponsored by Lately I've been working bash if exit code not 0 on a lot of automation and monitoring projects, a big part of these projects are taking existing scripts and modifying bash script exit on error 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
Bash Not Equal
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, 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 bash return value from function 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 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 cod
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 the program's robustness. That is, the program's bash set exit code ability to handle situations in which something goes wrong. Exit status As you recall from previous lessons,
Exit Code 0
every 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
Bash Exit On Error
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. It is also important that your scripts return a meaningful exit status when they http://bencane.com/2014/09/02/understanding-exit-codes-and-how-to-use-them-in-bash-scripts/ 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 and delete the files in that directory. That's the intended behavior. But what happens if the directory named in $some_directory doesn't http://linuxcommand.org/wss0150.php 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 * else echo "Cannot change directory!" 1>&2 exit 1 fi In this version, we examine the exit status of the cd command and if it's not zero, we print an error message on standard error and terminate the script with an exit status of 1
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 https://bash.cyberciti.biz/guide/The_exit_status_of_a_command 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 exit code 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 bash if exit 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 ec