Exiting from script when error occurs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exiting from script when error occurs
# 1  
Old 04-25-2008
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 Smilie) 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 Smilie

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...
Sreejith_VK
# 2  
Old 04-25-2008
Make directory Or exit
Code:
mkdir newdir_$sid || echo "Can not create newdir_$sid, exit now"; exit;

# 3  
Old 04-25-2008
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;"
Sreejith_VK
# 4  
Old 04-25-2008
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  
Old 04-25-2008
WOW!!
Thanks Jean..really greatfull to you for passing this info..
This is working fine....
Sreejith_VK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to execute condition until an event occurs

I need a script to keep polling "receive_dir" directory till "stopfile" get written in the directory. This has to run despite empty directory. So far i have this but fails if receive_dir is empty with no files with "unary operator expected". Help !! #!/usr/bin/ksh until do for i... (1 Reply)
Discussion started by: iaav
1 Replies

2. Shell Programming and Scripting

Script returning an error message on exiting

Hi, I am writing a script in which I am using an IF-Else statement. Code sample: # Check for the product. If (test "$3" = "Pet") Then Product_Code="PI" elif (test "$3" = "Breakdown") Then Product_Code="RI" elif (test "$3" = "Travel") Then Product_Code="TI" ... (2 Replies)
Discussion started by: bghosh
2 Replies

3. Shell Programming and Scripting

Exiting out of the script

I have to write a script in ksh which again should call another script. Say A.ksh is calling B.ksh. Now in B.ksh if the condition we are checking for is true then we have to go back to the main script A.ksh or if the condition in B.ksh is false then we have to totally come out of the scripts. I... (1 Reply)
Discussion started by: vpv0002
1 Replies

4. UNIX for Advanced & Expert Users

Truncation Occurs When Outputting Shell Script to stderr

Operating System: Solaris 10, Shell We are outputting the results of our scripts to the stderr file. However we have encountered a problem where some of the lines in the file are truncated. Is there a way to increase the terminal or column size within the script so that this does not... (4 Replies)
Discussion started by: fazzasx
4 Replies

5. HP-UX

ENOENT error occurs while issueing execlp command

/************************************************************************ * * TEST NAME: setrlimitd_su.c * * PURPOSE: To verify the soft (rlim_cur) and hard (rlim_max) limit * of process RLIMIT_DATA resources correctly inherited by * the exec() system call. * * RESULT: function call should... (1 Reply)
Discussion started by: mansa
1 Replies

6. Shell Programming and Scripting

exiting from script

Hi, I am trying to exit the script from a function. I was in assumption that if we use exit ( inside or outside the function) it will exit from the script. alternatively, return will exit from that particular function. but in my case, exit is exiting from the function and not the script.... (8 Replies)
Discussion started by: shellwell
8 Replies

7. Shell Programming and Scripting

Exiting a script

I have a script abc.sh. Its contents are as follows: (7 Replies)
Discussion started by: lassimanji
7 Replies

8. UNIX for Advanced & Expert Users

stty: error occurs when installing rpm

Hello All, when I install rpm rpm --install rpm packagename I got the following errors stty:standard input:invalid argument stty:standard input:invalid argument I dont know what i have to do exactly. I search on google for the same but not a particular standard solution is given... (17 Replies)
Discussion started by: amitpansuria
17 Replies

9. Shell Programming and Scripting

exiting from script

there are many script in my project.i am having a problem when i am trying to quit from child script.what is the command to wrap up all the parent script and calling script as well? exit 0 is not working.please help.... (1 Reply)
Discussion started by: arghya_owen
1 Replies

10. HP-UX

error occurs while some users login

hi all, i have a problem that while some of the users trying to login the following error occurs and the session is automatically closed. ssl error: RAND_status reported there wasn't enough randomness for the PRNG. ssl error: You need to specify RandomFile or EGDFile to obtain the randomness.... (0 Replies)
Discussion started by: rrlog
0 Replies
Login or Register to Ask a Question