1748249 Members
3795 Online
108760 Solutions
New Discussion юеВ

exit my scripts

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

exit my scripts

Hello,

I have created a script to ftp files to and from an sftp server.

I have a check function within the script:

# generic check function

check () {

if [[ $? -ne 0 ]] ; then

logfile "Unix Alert: Error Processing sftp01 MRD files, please investigate"
mail -s "$DATE:$TIME:$PROGPATH/$PROGNAME has failed, please investigate" chris@mail <$LOGFILE
rm $LCKFILE

fi

My question is I want to then exit out of the entire script so I put exit 1 after the rm $LCKFILE but this then continued within the script.

what is the exit code to stop the script processing completely within the check function?

many thanks
hello
12 REPLIES 12
Ivan Krastev
Honored Contributor

Re: exit my scripts

Try with :

exit 127


regards,
ivan
Yang Qin_1
Honored Contributor

Re: exit my scripts

Try:
# generic check function

check () {

if [[ $? -ne 0 ]] ; then

logfile "Unix Alert: Error Processing sftp01 MRD files, please investigate"
mail -s "$DATE:$TIME:$PROGPATH/$PROGNAME has failed, please investigate" chris@mail <$LOGFILE
rm $LCKFILE

CODE=1

fi

exit $CODE


Yang
Yang Qin_1
Honored Contributor
Solution

Re: exit my scripts

Sorry, forget to define CODE before "if"

# generic check function

CODE=0

check () {

if [[ $? -ne 0 ]] ; then

logfile "Unix Alert: Error Processing sftp01 MRD files, please investigate"
mail -s "$DATE:$TIME:$PROGPATH/$PROGNAME has failed, please investigate" chris@mail <$LOGFILE
rm $LCKFILE

CODE=1

fi

exit $CODE


Yang
James R. Ferguson
Acclaimed Contributor

Re: exit my scripts

Hi:

Your code should perform as you want if you call the 'check' function immediately after a command that you want to evaulate for success. If you need to do several commands, but check only the status of the first command later in your code, you would need to do something like:

#!/usr/bin/sh
check () {
if [[ $1 -ne 0 ]] ; then
echo "non-zero; exiting..."
exit 1
fi
}
cp #...produces a non-zero return...
X=$?
date
check ${X}
echo "oops!"
exit 0

...Are you sure that your code meets this requirement?

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: exit my scripts

here is an example of my code:

set_env

check () {

if [[ $? -ne 0 ]] ; then

logfile "Unix Alert: Error Processing sftp01 MRD files, please investigate"
mail -s "$DATE:$TIME:$PROGPATH/$PROGNAME has failed, please investigate" chris@mail <$LOGFILE
rm $LCKFILE

fi

}

logfile () {

MSG_STRING="$*"
echo "$DATE:$TIME: $MSG_STRING" >> $LOGFILE

}


ftp code .......... >> ftp script

./ftpscript
check

ftp code .......... >> ftp script2

./ftpscript2
check

ftp code ........... >> ftp script3

./ftpscript3
check

logfile " Finished processing ftp "


If the script encounters any error's then I want it to exit here however I have noticed ftp1 failing then the check mails abd the script will then continue.


hello
James R. Ferguson
Acclaimed Contributor

Re: exit my scripts

Hi (again):

How does your FTP script determine the success for failure of the FTP it does?

If you are relying on a non-zero return value from an FTP to denote a failed FTP, then that is a problem.

Your FTP script needs to examine the result of its transfer in the reply it returns. As documented in the 'ftpd(1M)' manpages, this is a three-digit code. For example, a value of 226 might indicate a successful transfer. You need to capture this dialog; examine it; and construct your own zero (0) or one (1) exit status from your FTP script. In that fashinon, your 'check' function will work as expected.

Regards!

...JRF...
Yang Qin_1
Honored Contributor

Re: exit my scripts

set_env

RET=0

check () {

if [[ $? -ne 0 ]] ; then

logfile "Unix Alert: Error Processing sftp01 MRD files, please investigate"
mail -s "$DATE:$TIME:$PROGPATH/$PROGNAME has failed, please investigate" chris@mail <$LOGFILE
rm $LCKFILE

RET=1

fi

}

logfile () {

MSG_STRING="$*"
echo "$DATE:$TIME: $MSG_STRING" >> $LOGFILE

}


ftp code .......... >> ftp script

./ftpscript
check

if [ $RET -ne 0 ] ; then
exit 1
fi

ftp code .......... >> ftp script2

./ftpscript2
check

if [ $RET -ne 0 ] ; then
exit 1
fi

ftp code ........... >> ftp script3

./ftpscript3
check

if [ $RET -ne 0 ] ; then
exit 1
fi

logfile " Finished processing ftp "


Yang
Peter Nikitka
Honored Contributor

Re: exit my scripts

Hi,

there's no need of global variables - set a return value for your check function:

check () { [ $1 -eq 0 ] && return 0
logfile ...
mail -s ...
rm ...
return $1
}

create-ftpscript >ftpscript1

ftpscript1
if ! check $?
then exit $? # this will be the ret.val of check()
fi

...


mfG Peter




The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
lawrenzo_1
Super Advisor

Re: exit my scripts

James,

Thanks for your comments, I am not particulaly interested in the FTP'd file itself because if the file transfer is a failure then it will be picked up again elsewhere.

I am looking for failures within the transfer process - ie if one ftp failed then the script must stop so when run next time the remaining files processed.

I will attach the script as it is now.

Pete,

can you elaborate on your comments as I am not sure of the syntax here - as I can see yang has the solution?

Thanks for your help - if you have any idea's then I will take them onboard.

cheers
hello