![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 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 $?
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... |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Make directory Or exit
Code:
mkdir newdir_$sid || echo "Can not create newdir_$sid, exit now"; exit; |
|
#3
|
|||
|
|||
|
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:
|
|
#4
|
||||
|
||||
|
If you just want tio exit from the script when an error occurs, add the following command :
Code:
set -e 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!" |
|
#5
|
|||
|
|||
|
WOW!!
Thanks Jean..really greatfull to you for passing this info.. This is working fine.... |
|||
| Google The UNIX and Linux Forums |