C Shell Script Error Handling
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 between a good applescript do shell script error handling program and a poor one is often measured in terms of the program's
Error Handling In Unix Shell Script
robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status As you recall from shell script exit code 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 is anything other than zero, then bash error handling 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 code: # Example of a really bad idea cd
Exit Bash Shell
$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 with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false
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 bit fast and loose with my shell scripts over the last few bash if exit code months, because I haven't talked about how to ensure that error conditions don't break things. shell script exit on error 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!
Bash Script Exit On Error
This topic ranges from the simple to the sophisticated, so let's start with a basic test: the return status after an application or utility is invoked. The Magical $? Sequence Different shells have different return status indicators (the C shell, http://linuxcommand.org/wss0150.php 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 "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: http://www.linuxjournal.com/magazine/work-shell-handling-errors-and-making-scripts-bulletproof 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 follows if I invoke it three times—twice in error situations and once without an error: mkdir: /: Is a directory return status is 1 mkdir: /tmp/foobar: File exists return status is 1 It's a drag to have mkdir generate an error message when you can produce your own simply by testing the $? status variable. Here's how you can do just that: makedirectory() { mkdir $1 2>&1 > /dev/null status=$? e
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 http://stackoverflow.com/questions/31213467/how-to-catch-errors-or-exceptions-in-csh hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation 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 only takes a minute: Sign up How to catch errors or exceptions in Csh? up vote 1 down vote favorite I would like to know if there is a way to catch the exceptions and shell script control the flow when this happen. For example running this line, I would like to know if a shell error occurred . source /scripts/scriptThatWillFail.csh linux shell csh share|improve this question asked Jul 3 '15 at 19:54 Leo 7711224 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted The exit/return code is stored in the status variable in C shell. source /scripts/scriptThatWillFail.csh if ($status != 0) then echo failed else echo passed endif shell script error share|improve this answer edited Jul 9 '15 at 22:32 answered Jul 3 '15 at 20:01 Eugeniu Rosca 4,027528 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged linux shell csh or ask your own question. asked 1 year ago viewed 216 times active 1 year ago Blog Stack Overflow Podcast #89 - The Decline of Stack Overflow Has Been Greatly… Related 219Can a shell script set environment variables of the calling shell?578How to set a variable equal to the output from a command in Bash?0What does the -b argument do in csh?3csh inline math2How to run a csh script from a sh script0csh shell script troubleshooting0How to show line number when executing csh script?1Shell Script (Csh shell) send attachement0How to define 'alias' in csh1Csh echo foreach loop index Hot Network Questions how to protect against killer insects Why is a spacetime with negative curvature assumed to have a hyperbolic, rather than spherical, geometry? Why is HTTP data sent in clear text over password-protected Wifi? Red Herring Bonkers In The Red Herring Bunkers easyJet