Error Handling In Linux Shell Scripting
Contents |
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
Exception Handling In Shell Script Linux
between a good program and a poor one is often measured in terms file handling in linux shell script of the program's robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status linux shell scripting pdf As you recall from previous lessons, 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
Linux Shell Scripting Cookbook Pdf
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 finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of
Learn Linux Shell Scripting
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 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 wit
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 linux shell scripting commands Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Linux Shell Scripting Tutorial Pdf
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, linux shell scripting examples just like you, helping each other. Join them; it only takes a minute: Sign up Error handling in BASH up vote 143 down vote favorite 109 What is your favorite method to handle errors in BASH? The http://linuxcommand.org/wss0150.php best example of handling errors in BASH I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org. William Shotts, Jr suggests using the following function for error handling in BASH: #!/bin/bash # A slicker error handling routine # I put a variable in my scripts named PROGNAME which # holds the name of the program being run. You can get this # value from the first item on the command http://stackoverflow.com/questions/64786/error-handling-in-bash line ($0). # Reference: This was copied from
the ability to anticipate and respond to error situations gracefully and without anything breaking. Dave explores some of the basic shell script error-handling options. I realize I've been playing a http://www.linuxjournal.com/magazine/work-shell-handling-errors-and-making-scripts-bulletproof bit fast and loose with my shell scripts over the last few months, https://www.dssw.co.uk/blog/2012-10-08-how-to-add-error-checking-to-your-shell-scripts/ because I haven't talked about how to ensure that error conditions don't break things. If you read the Letters section in Linux Journal, you know I haven't covered this topic because, well, you have covered it for me! This topic ranges from the simple to the sophisticated, so let's start with a basic test: shell script the return status after an application or utility is invoked. The Magical $? Sequence Different shells have different return status indicators (the C shell, for example, uses $status), but the most basic is Bash/the Bourne shell, which is what we've focused on since I started writing Work the Shell, and it uses $?. Here's a quick example: #!/bin/sh mkdir / echo "return status is $?" mkdir /tmp/foobar echo linux shell script "return status is $?" rmdir /tmp/foobar echo "return status is $?" rmdir /tmp echo "return status is $?" exit 0 Run this, and you can see the difference between commands that succeed and those that fail: mkdir: /: Is a directory return status is 1 return status is 0 return status is 0 rmdir: /tmp: Not a directory return status is 1 You can see that when invoking mkdir or rmdir with an error condition, they output an error and—the important part—the $? return status is nonzero. In fact, check out the man page for a typical command like mkdir, and you'll see: “DIAGNOSTICS: The mkdir utility exits 0 on success, and >0 if an error occurs.” In a perfect world, the >0 return code would actually tell you what happened, but although that's true with the functions accessible via software, it's not true for the shell. On the other hand, it's still helpful to explore how to make a shell function that does error handling too. Here's a basic example function: makedirectory() { mkdir $1 status=$? echo "return status is $status" } This just makes a simple function that calls mkdir, and it should be no surprise that it works as
article shows how to add error checking to highlight problems you might otherwise miss. Access the shell on Mac via the Terminal.app Unlike more recently designed languages, shell script does not have an easy answer for error handling. There are no common exception handling routines or ways of wrapping up large blocks of script and asking for errors to fall through to a provided subroutine. Instead shell script asks you, the author, to check individual program exit codes and branch as needed in case of an error. In practice, this means your once short script is going to get a little longer and a little more involved. Unix Exit Status Each program you run through a shell script returns an exit status. This numeric status value tells the calling script if the program completed successfully or if an error was encountered. The exit status is not visible on the command line. This makes it difficult to tell if something went wrong just by looking at the textual output of a shell script. It is possible - even common - for scripts to print nothing and yet encounter multiple errors. Let's consider this simple script: #!/bin/sh cp /Volumes/Documents/criticalfile.txt /Volumes/BackUp/. This script does one thing; it copies a single file from one volume to another using the cp program. Thankfully the cp program is chatty and will print a message if an error is encountered. That is great for us reviewing the output visually, but for the shell running our script the error will go completely unnoticed. This is a problem if our script goes on to do more work, or if we want the script to robustly deal with errors. Let's add error checking to this simple script. #!/bin/sh cp /Volumes/Documents/criticalfile.txt /Volumes/BackUp/. if [ "$?" != "0" ];