1834051 Members
2575 Online
110063 Solutions
New Discussion

Exit Script on failure?

 
SOLVED
Go to solution
Duffs
Regular Advisor

Exit Script on failure?

Hi,

I have written sub script that sends (rcp's) a certain type of file to another server. If by any chance the line drops however this file will be put into an archive dir resulting in the file not being sent. Can anyone tell me how I can incorporate a err/exit code into my script so that if the rcp fails then the entire script will exit at that point peventing the file being archived and ensuring that it will get rcp'ed the next time it is run from the cron.

Dermot
10 REPLIES 10
Devender Khatana
Honored Contributor

Re: Exit Script on failure?

Hi,

Can you attach the script & the cron entry which is doing this?

Regards,
Devender
Impossible itself mentions "I m possible"
Duffs
Regular Advisor

Re: Exit Script on failure?

Please find the attached script.

This sub script is called as part of a larger script that is kicked off the cron every 5 mins.
Devender Khatana
Honored Contributor

Re: Exit Script on failure?

Hi,

Could not open the attachment. Please reattach.

Regards,
Devender
Impossible itself mentions "I m possible"
Bharat Katkar
Honored Contributor

Re: Exit Script on failure?

Hi Dermot,
This should work :

.....
rcp ....
var1=`echo $?`
if [ $var1 -ne 0 ]
then
exit
fi


You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: Exit Script on failure?

You can get return value from rcp command after it's execution as,


rcp :/tmp/test/*.sh /tmp/test/
if [[ $? -eq 0 ]]
then
echo "success"
...action..
else
echo "failure"
fi

hth.
Easy to suggest when don't know about the problem!
Duffs
Regular Advisor

Re: Exit Script on failure?

These error messages only apply to the sub-script, they don't actually transfer to the main script therefore the program doesn't actually exit but continues excuting the remaining sub scripts instead of bombing out.

I really need to know how to transfer an error message from one script (this sub script) to another script (my main script).

Dermot
Muthukumar_5
Honored Contributor
Solution

Re: Exit Script on failure?

I have read your script but with un-need codes more there.

#!/bin/ksh
# subscript.ksh
rcp server2:
if [[ $? -eq 0 ]]
then
echo "Success"
else
echo "failure"
exit 1
fi

exit 0

######################

#!/bin/ksh
#main.ksh
sh subscript.sh
if [[ $? -eq 0 ]]
then
echo "Subscript is successful"
else
echo "Subscript is failed"
exit 1
fi
exit 0
hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Exit Script on failure?

You can also use global log file to log subscript informations.

#!/bin/ksh
#subscript.ksh
LOGFILE=/tmp/subscript.log
rcp ..
if [[ $? -eq 0 ]]
then
echo "success" > $LOGFILE
else
echo "failed" > $LOGFILE
fi

## Main script ###
#!/bin/ksh
#main.ksh
LOGFILE=/tmp/subscript.log
ksh subscript.ksh
if [[ -f $LOGFILE ]]
then
grep -q success $LOGFILE
if [[ $? -eq 0 ]]
then
echo "Subscript execution is success"
else
echo " ...failed"
fi
fi

hth.
Easy to suggest when don't know about the problem!
Duffs
Regular Advisor

Re: Exit Script on failure?

Thanks a million, that did the trick, much appreciated!

Kind Regards,
Dermot
Duffs
Regular Advisor

Re: Exit Script on failure?

Hi,

As it turns out the above solution is only half the solution as it does not completely solve my problem. In the instance where there are no files to rcp, the script returns an error value of 1 thus bombing out instead of returning value zero and continuing to the next sub script.

Is there a way I can catalog the dir, i.e. run a 'ls' or 'read' on the dir and if there are no files present simply return a 0 and continue with the remainder of the script instead of exiting?

Dermot