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
This is the code i am currently using, but the error handling doesnt seem to be working.
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:
Last edited by agama; 08-04-2012 at 04:45 PM..
Reason: added comment; cleaned up just a bit
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:
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:
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.
If you don't need the unsuccessful exit status of the failed command that you started in the background, you can change:
to:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)