The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
exiting from script arghya_owen Shell Programming and Scripting 1 06-02-2008 03:36 AM
error occurs while some users login rrlog HP-UX 0 08-12-2007 01:55 PM
Script Not Exiting??? lesstjm Shell Programming and Scripting 1 07-11-2007 08:58 AM
PHP5 Script 'Freeze' before exiting Unbeliever Shell Programming and Scripting 4 05-10-2007 08:32 AM
Shell script not exiting Gracefully smithK Shell Programming and Scripting 5 02-08-2007 03:48 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-24-2008
Sreejith_VK
Guest
 

Posts: n/a
Exiting from script when error occurs

Hi Friends,

Is it possible to exit nicely(ie, to echo a message with the error occurred) from a shell script(quiet a big one ) once it encounter an error in between the lines?

For example, in my script I am calling the command mkdir and sometimes (when the directory already exists) it fails.then i want my script to exit with the error message.Currently it passes to the next command.

I tried using the below function at the start of the script:

Code:
#!/bin/sh
check_errs()
{
  if [ "${1}" -ne "0" ]; then
    echo "ERROR # ${1} : ${2}"
    exit ${1}
  fi
}

#main script
mkdir newdir_$sid
check_errs $?
but calling this function after every command sounds

Can any one give me pointers on how to call this function after every command executed or someother method by which i can exit from program as soon as an error occurs...
Reply With Quote
Forum Sponsor
  #2  
Old 04-24-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 736
Make directory Or exit
Code:
mkdir newdir_$sid || echo "Can not create newdir_$sid, exit now"; exit;
Reply With Quote
  #3  
Old 04-25-2008
Sreejith_VK
Guest
 

Posts: n/a
Thanks a lot danmero..
This is working fine...

Is there any way of using trap commad and do the same?So that I dont have to use everywhere
Quote:
"|| exit;"
Reply With Quote
  #4  
Old 04-25-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
If you just want tio exit from the script when an error occurs, add the following command :
Code:
set -e
You can also specify a trap routine :
Code:
trap 'echo "Error detected! End of script.";exit 1' ERR
#set -e
argr="$1"
if [ -n "$arg" ]
then
   cat $arg.err
else
   echo "Empty arg"
fi
echo "done!"
Jean-Pierre.
Reply With Quote
  #5  
Old 04-25-2008
Sreejith_VK
Guest
 

Posts: n/a
WOW!!
Thanks Jean..really greatfull to you for passing this info..
This is working fine....
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:16 PM.


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

Content Relevant URLs by vBSEO 3.2.0