The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > UNIX and Linux Applications
Google UNIX.COM


UNIX and Linux Applications Questions involving software not covered by other forum go here. This includes Databases and Middleware.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to detect failure from batch SFTP and SHELL operations chengwei Shell Programming and Scripting 2 05-28-2008 04:32 AM
copy command failure mynix Shell Programming and Scripting 1 12-15-2007 08:35 PM
find command to detect installations goldenglobe UNIX for Dummies Questions & Answers 2 06-13-2007 07:06 AM
Automatically executing a command robbiegregg UNIX for Dummies Questions & Answers 8 04-06-2005 08:46 AM
failure of at command DKuester UNIX for Dummies Questions & Answers 5 10-10-2003 09:58 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Apr 2008
Posts: 1
How to automatically detect command failure

I have a shell script. In this script I executes various command and my requirement is such that if any command fails I've to terminate the shell script.

To achieve this objective I'm checking the value of $? after each command and if its value is greater thaen I 'exit' the script.

Is there any way through which I can avoid the if condition on $? after each and every command as It a pain to put if condition after every command.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 155
You can write a function, say "handle_error" where you can define the if condition, and execute this function whenever you need it.
Reply With Quote
  #3 (permalink)  
Old 04-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,111
Code:
set -e
This will make the script terminate on any unhandled error.

It's more picky than you think, so a script which was not written to cope with this probably has unchecked commands which might bite you in the back. (It's good for the self-discipline, of course.)

For example, anything like hello && echo success will terminate the script if hello fails. You need to rewrite that as an if ... then, or artificially add || true at the end.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0