(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Breaking out of a script


2009/03/25





Sometimes you just want to bail out of a script when something happens. Let's say we're testing the output of some "chk" command and want to exit if it says "No". That's easy: we just add "chk | grep -q "No" && exit 0" to our script. If that "chk" spits out "No", our script is done right then and there - no more of it will be executed.

But what if you want to do more? You want to say "Sorry, Dave, I can't do that" and then exit. This won't work :


chk | grep -q "No" && echo "Sorry Dave"; exit 0;
echo "rest of script"
 

That's always going to exit, whether "chk" says "No" or "Fooey"

You can do this:

chk | grep -q "No" && echo "Sorry Dave" && exit 0;
echo "rest of script"
 

That's kind of a cheat - it relies on your echo "Sorry Dave" returning success. Nothing wrong with that but it gets kind of clumsy when you want to make a bunch of other things happen after the apology. It gets impossible if you have to use something that won't necessarily return success:

chk | grep -q "No" && echo "Sorry Dave" && ls /adir  && exit 0;
echo "rest of script"
 

That only works if /adir exists. There has to be an easier way.

Wait, can't we put things in lists? Sure we can! How about:

chk | grep -q "No" && ( echo"Sorry Dave";echo "You too, John";exit; )
echo "rest of script"
 

That almost works - it apologizes to both people, but it doesn't exit. How can that be?

The exit doesn't happen because that kind of list runs in a sub-shell. It's exactly like putting echo"Sorry Dave";echo "You too, John";exit; in another script called "sorry" and running:

chk | grep -q "No" &&  sorry
echo "rest of script"
 

Fortunately, bash has a solution:

chk | grep -q "No" && { echo"Sorry Dave";echo "You too, John";exit; }
echo "rest of script"
 

The braces tell bash to execute the list right here in the same shell. The exit will work properly now. Don't leave off the ";" though; you'll get " syntax error: unexpected end of file" if you do.

There are other ways to do this.

result=`chk | grep -q "No"`
if [ $result ] 
then
  echo"Sorry Dave"
  echo "You too, John";
  exit
fi
echo "rest of script"
 

Or this way:



;


function apology {
  echo "Sorry, Dave."
  echo "You too, John"
  exit
}

chk | grep -q "No" &&  apology
echo "rest of script"
 

As a general rule, you shouldn't put exits in functions - doing that makes a complicated script harder to debug, especially because different languages treat an "exit" in different ways. The guy reading your script may think that the "exit" applies only to the function and not the whole script. Yes, he'd be wrong, but when you work with several languages it's easy to forget these details. Best not to put a root like that in his path.

Right this second, I can't think of another way to do this. Can you?


;


Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar


ad

cartoon

numly esn 26821-090325-185341-97
numly barcode

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Basics
       - Shell
       - MacOSX
       - Linux


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress