Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make script error out

Status
Not open for further replies.

jaburke

Programmer
May 20, 2002
156
US
I know this is simple, but I am rusty with my ksh scripting. I have a script that is doing an ftp to a mainframe and deleting a file. I have a grep statement in the script to look at the logfile. If there's an error in the log, I want the script to exit with an error. I can't make this work. It will exit, but without an error. I have tried it all sorts of ways. Here's the end of the script with one of my tries. Help!

rcTest='grep -i fails logfile'
if [[ ${?} = 0 ]] then
err=1
fi

 
Why not simply this ?
Code:
grep -i fails logfile && err=1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Setting "[tt]err=1[/tt]" in the script is fine for within the script, but when you exit, that variable and it's value won't be captured. You need to exit with a return code. Something like...
Code:
#!/bin/ksh
# mainscript.sh

# Do your FTP and deleting here

grep -i fails logfile && [b]exit 86[/b]

# More commands here if there is no error in teh grep

[b]exit 0[/b]
Then, if another script calls it...
Code:
#!/bin/ksh
# anotherscript.sh

# Run the main script
/path/to/mainscript.sh

# Capture the return code
STATUS=$?

# Do something with it
if (( STATUS ))
then
    print "WARNING! mainscript.sh returned an error code of ${STATUS}"
    print "Commence panicking now!"
fi
Is that what you're looking for?

 
Is this an attempt to add some sort of error handling to the ftp script?

If you don't have a lot invested in this script already, you might consider performing this action in perl. The Net:FTP bundle will already deal with errors.

The only reason I suggest this, is because I was working on a similar script using bash, until I have up and converted it to perl. It turned out a lot shorter, a lot simpler and more reliable.
 
vortmax makes a good suggestion. I find that most FTP tasks can be replaced with SFTP, or better yet, just SCP. That makes it a lot easier and cleaner to test for errors.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top