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 > 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 and shell scripting languages 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 06:36 AM
error occurs while some users login rrlog HP-UX 0 08-12-2007 04:55 PM
Script Not Exiting??? lesstjm Shell Programming and Scripting 1 07-11-2007 11:58 AM
PHP5 Script 'Freeze' before exiting Unbeliever Shell Programming and Scripting 4 05-10-2007 11:32 AM
Shell script not exiting Gracefully smithK Shell Programming and Scripting 5 02-08-2007 06:48 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 Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
Sreejith_VK
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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...
  #2 (permalink)  
Old 04-25-2008
danmero danmero is offline Forum Advisor  
  
 

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

Posts: n/a
Bits: 0 [Banking]
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;"
  #4 (permalink)  
Old 04-25-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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.
  #5 (permalink)  
Old 04-25-2008
Sreejith_VK
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
WOW!!
Thanks Jean..really greatfull to you for passing this info..
This is working fine....
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 12:19 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