The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Special Forums > UNIX and Linux Applications
.
google unix.com



UNIX and Linux Applications Discuss UNIX and Linux software applications. This includes SQL, Databases, Middleware, MOM, SOA, EDA, CEP, BI, BPM and similar topics.

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 07:32 AM
copy command failure mynix Shell Programming and Scripting 1 12-16-2007 12:35 AM
find command to detect installations goldenglobe UNIX for Dummies Questions & Answers 2 06-13-2007 10:06 AM
Automatically executing a command robbiegregg UNIX for Dummies Questions & Answers 8 04-06-2005 11:46 AM
failure of at command DKuester UNIX for Dummies Questions & Answers 5 10-10-2003 12:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 04-24-2008
ashok2008 ashok2008 is offline
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.
  #2 (permalink)  
Old 04-24-2008
krishmaths krishmaths is offline
Registered User
  
 

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

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0