Visit The New, Modern Unix Linux Community


Help me add Error Handling to my script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me add Error Handling to my script
# 1  
Help me add Error Handling to my script

Hi all,

I have some sections of a script that I am trying to add error handling to. Basically if it returns any error, just continue. This is for a deployment script that i am writing to assist in the deployment of scripts out to other systems.

At the top of my KSH script i added this function

Code:
on_error_continue()
{
if [[ $? -ne 0 ]]; then
print "$1"
#exit $?
continue
fi
}

This is the code i am currently using, but the error handling doesnt seem to be working.

Code:
for hostname in cat hostfile
do 
rcp $DEPLOY $hostname:/var/tmp/$TYPE
on_error_continue "Could not remote copy"
rsh $hostname "nice -n 39 ksh /var/tmp/$TYPE &"
on_error_continue "Could not mount the NAS"
done

Can someone point me in the right direction?
# 2  
A couple of things that jump out at me immediately. First, what you intend to be a cat command to use as input to your loop won't actually execute the command. You'd need to put it into a form that the shell will interpret as 'execute this', but there is a better way.

Secondly, be careful using $? anywhere other than immediately following the execution of a command. In your continue function if you were try to use your exit statement, the value of $? would be the return of the print, not the value you intended.

Lastly, I would ensure that at least the host name is output in any message. It would be very frustrating as an operator/user to have the message "can't start NAS" and not know where.

This isn't exactly how I would have done it, but keeping with your general theme this should do what you intend:

Code:
# pass in the status to check as $1; the res of the parameters are the message if
# the status wasn't good
#
function on_error_continue
{
    rc=$1           # snag the exit code passed in

    if (( $rc > 0 ))
    then
        print "$2"
        #exit $?        # using $? here will be the exit code of the print; use exit $1 
    fi
}

#for hostname in cat hostfile   # this will not work -- it will run the loop for "cat" and "hostfile" and won't cat the hostfile
while read hostname
do
    rcp $DEPLOY $hostname:/var/tmp/$TYPE
    on_error_continue $? "Could not remote copy to $hostname"
    rsh $hostname "nice -n 39 ksh /var/tmp/$TYPE &"
    on_error_continue $? "Could not mount the NAS on $hostname"
done <hostfile   # read directly from hostfile; no need to use cat


Last edited by agama; 08-04-2012 at 04:45 PM.. Reason: added comment; cleaned up just a bit
# 3  
Of the rcp(1) man pages listed on this site, only the Solaris man page specifies exit codes. That man page also has the following warning:
Quote:
When rcp is used to perform third-party copies where either of the
remote machines is not running Solaris, the exit code cannot be relied
upon. That is, errors could occur when success is reflected in the exit
code, or the copy could be completely successful even though an error
is reflected in the exit code.
Your function looks OK, but it can't work if the command it follows doesn't reliably set its exit status.
This User Gave Thanks to Don Cragun For This Post:
# 4  
It just dawned on me that you probably intended to skip the rsh if the rcp fails. Given that, this seems less confusing to me:

Code:
while read hostname
do
    if !  rcp $DEPLOY $hostname:/var/tmp/$TYPE
    then
        print "Could not remote copy to $hostname"
        continue
    fi

    if !  rsh $hostname "nice -n 39 ksh /var/tmp/$TYPE &"
    then
        print "Could not mount the NAS on $hostname"
    fi
done <hostfile

EDIT: Given Don's observation, (I use ssh/scp so wasn't aware of the rcp limitation) this may not behave as expected. Using scp and ssh would certainly be an advantage if it is possible to use them.
# 5  
Sorry my cat command i did not write verbatim, so lets ignore that.

So it sounds like there is a better way to do this. I dont mind starting over, because i want this to work as smoothly as possible.

Assuming I just wanted to run a single command
Code:
rsh host ksh scriptname &

What would be the best way to handle the error?
# 6  
Code:
wait;ec=$?
if [ "$ec" -ne 0 ]
then
   <do whatever you want to report the error>
else
    <do whatever you want to report successful completion>
fi

If you don't need the unsuccessful exit status of the failed command that you started in the background, you can change:
Code:
wait;ec=$?
if [ "$ec" -ne 0 ]

to:
Code:
if wait

But, as I said before, if the version of rcp you use doesn't reliably set its exit status, you're out of luck trying to determine whether an invocation of rcp succeeded unless you're lucky enough to capture an error message written to stderr.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #932
Difficulty: Easy
Most first-generation personal computers did not keep track of dates and times.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script FTP maintain error handling

Hi, I have ftp script like below How to insert an error handling, If the transfer failed then send mail to me. Actually, I just need the script to send an email if the FTP failed. How to put the email script within FTP script? Thank You Edy (5 Replies)
Discussion started by: edydsuranta
5 Replies

2. Shell Programming and Scripting

error situation handling when starting app from a script

Hi, in my bash script I start 3rd party app that runs quickly normally and returns some data that I use later for processing. However if there are problems the app doesn't return anything and just hangs... then my script also hangs of course, awaiting for the app to comeback. Was wondering how to... (3 Replies)
Discussion started by: TomSu
3 Replies

3. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

4. Shell Programming and Scripting

Expect Script Error Handling

Good Day Everyone, I was hoping to get a little insight into an expect script that I've written. Basically we have this expect script to perform an sftp upload, key authentication is not an option, and sftp is the only method supported by our vendor, thus the need for this. I want to be... (3 Replies)
Discussion started by: thaller
3 Replies

5. Shell Programming and Scripting

Error Handling

Below code works for different databases i.e. MYSQL and ORACLE The problem is for MYSQL in Block: if ; $? taking value accordingly but in case of ORACLE $? is always taking this value as zero (0). That is the reason in Oracle it always going in else Block in any case.. :( and in case of ... (4 Replies)
Discussion started by: ambarginni
4 Replies

6. Shell Programming and Scripting

PERL error handling

I have a PERL command line embedded in a UNIX script. The script doesn't handle errors coming out of this command. I'm processing large files and occassionally I run out of disk space and end up with half a file. perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4 How do I handle errors coming out... (1 Reply)
Discussion started by: OTChancy
1 Replies

7. Shell Programming and Scripting

Help with Error Handling on Script

Hi, I need your guys help again. I run a script which check for some process status in a loop. when i check the process some of the process could throw an error, how can i check that inside my script. Thanks, RR (3 Replies)
Discussion started by: rrb2009
3 Replies

8. Shell Programming and Scripting

Issue with Error handling,not able to continue the script further

Hi, I am trying to write a script to cleanup files in a log directory .. cd log find Datk** -mtime +7 -exec rm -f {} \; 2> /dev/null Have used the above to clean up files in log directory more then 7 days older. The file can be something like ( auto-generate by some processes and... (2 Replies)
Discussion started by: nss280
2 Replies

9. Shell Programming and Scripting

Finger and error handling

I have this segment of code : cmd = "finger -m " $1 " 2>/dev/null | head -1" cmd | getline userinfo close(cmd) Sometimes finger returns no such user when given a user id. With the redirection to the default trash file i am getting rid of any screen "finger:no such user" messages. I also want... (2 Replies)
Discussion started by: beatblaster666
2 Replies

10. Shell Programming and Scripting

Error Handling

Helo Experts, I need a help in handling errors in shell script, wants my errors displayed in text file instead of command window.. My shell script is here; cd /cygdrive/s/Files for FILES in ./*.* do temp=`basename $FILES` if cp $FILES /cygdrive/r/CopyFile1/$FILES; then echo "copy... (5 Replies)
Discussion started by: CelvinSaran
5 Replies

Featured Tech Videos