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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Statement in UNIX - Trapping Errors

Status
Not open for further replies.

dayankoven

Programmer
Oct 28, 2002
17
MY
Hi there fellow experts,

I'm very new to UNIX shell programming and I'm trying to copy a file from an ftp directory to a working directory for processing. The script will accept 2 parameters, one is the filename and the other is the SYSTEM code. The script below works fine when both the parameters passed in are correct. But, when wrong parameters are passed in, the script doesn't throw any message but just executes perfectly. Is there something can i do, to say if there is an error, exit with return code != 1 or throw an error message.

A sample Filename will be ABC.

So, i execute the script with the following command :-
APPL.sh ABC SRS

If ABC is found at /sftp/ftpsrs/SRS/CRM/input/ it will be copied over to /prodlib/CRM/batch_dev11/working/ with the timestamp attached.

If i execute the script with the following command :-
APPL.sh XYZ PPP

The script should throw an error and exit with return code non zero but currently it doesn't.

Would appreciate if someone could help me with this. Thanks a lot. My codes are shown below



mydate=$(date +"%Y%m%d%H%M%S")
exception="00"
myfilename=$1
mypath=$2

if [ $2="SRS" ]
then
if test -f /sftp/ftpsrs/SRS/CRM/input/$1
then
cp -p /sftp/ftpsrs/SRS/CRM/input/$1 /prodlib/CRM/batch_dev11/working/$myfilename$mydate$exception.dat

else
RC=1
fi
fi


if [ $2="AIS" ]
then

if test -f /sftp/ftpais/AIS/CRM/input/$1
then
cp -p /sftp/ftpais/AIS/CRM/input/$1 /prodlib/CRM/batch_dev11/working/$myfilename$mydate$exception.dat

else
RC=1
fi

fi

if [ $2="CLS" ]
then

if test -f /sftp/ftpcls/CLS/CRM/input/$1
then
cp -p /sftp/ftpcls/CLS/CRM/input/$1 /prodlib/CRM/batch_dev11/working/$myfilename$mydate$exception.dat

else
RC=1
fi

fi

 
#!/usr/bin/ksh

mydate=$(date +"%Y%m%d%H%M%S")
exception="00"
myfilename=$1
mypath=$2
typeset -l lpath=$mypath #lower case

if test -f /sftp/ftp${lpath}/$mypath/CRM/input/$myfilename
then
cp -p /sftp/ftp${lpath}/$mypath/CRM/input/$myfilename /prodlib/CRM/batch
_dev11/working/$myfilename$mydate$exception.dat
return 0
else
return 1
fi
##--------------------
I've simplified the script, just to show (what I think) is better. You'll notice that once the positional parameters have been assigned to a variable, I no longer refer to them as $1 $2.
You don't need specific tests for each path, as the logic is the same for all paths. (unless I missed something)
HTH
 
There's a small but significant mistake in my post.

I've been writing functions this week, and my brain's in the wrong gear.

"return" is used when returning from a function.

"exit" is used when exiting from a script.

So the above should be :-

exit 0
else
exit 1

You can process the return code in 2 ways

$? contains the return code

APPL.sh ABC SRS
if [ $? -ne 0 ]
then
do failed stuff
fi

or you can use true/false (&& true, || false)

APPL.sh ABC SRS ||
{
do failed stuff
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top