Link to home
Start Free TrialLog in
Avatar of sreeramreddy
sreeramreddy

asked on

Shell Scripts

Hi,

We have few shell scripts which are executed from cron daily, but one script is dependant on the out come of the privious one. Can you please let me know the code to capture the success and failure of the first script and depending on that to execute the second script. If success execute the second script other wise don't.

Thank You,

Sreeram
Avatar of chris_calabrese
chris_calabrese

I'm guessing what you want is something like

24 02 * * * /script1
24 03 * * * if script1 was successful, then /script2

Unfortunately, cron does not directly support this kind of syntax.  What you can do, however is something like this:

24 02 * * * /script1 && /script2

This works because cron simply calls the shell to execute the scripts and this will cause the shell to first exeucte /script1 and then later do /script2.

If you really need script2 to execute at a certain time, you can get really fancy and do something like

24 02 * * * /script1 && at 03:24 /script2
I thought of 2 possible ways, one with temporary file and
the other, just put all the scripts in one big script.

1) Using temporary files:

Script one:
========

rm -f *.ok *.bad

# DO YOUR STUFF HERE
# IF OK create step1.ok
# ELSE create step1.bad
exit

Script two:
=======

if [ -f step1.ok ]
then
  rm -f step1.ok
#DO YOUR STUFF HERE
# IF OK create step2.ok
# ELSE create step2.bad
else
  exit 1
fi

Scrip three will be similar to script two.

2) In one big script:

script_one
if [ $? -eq 0 ]
then
  script_two
  if [ $? -eq 0 ]
  then
    script_three
    # and so forth
  fi
fi

Hope this helps.
Avatar of sreeramreddy

ASKER

I am looking something like what szetoa suggested, but I am not able to capture the success of the first script and depending on that execute the second one. after executing a script when I am trying to get the out come like 0 or 1 I am not able to do that. We are using Solatis 2.6, sh shell.

Thanks,

Sreeram.
I tried the second sample, but I am not able to capture the outcome of the script. It is giving a message variable syntax.
In the first sample using temporary files, you don't check the successful of the script execution, you check the existence of the .ok or .bad file.

In the second sample using a big script, make sure the inner scripts do exit with a code (0 successful, otherwise failed)
Can you show how to exit your first script?  If you use exit 0, the second script should capture the return code.
I am using the folling script to test with txt files and with out text files and one more script calling this script:

INNER script:

#!/bin/sh

if [  -s $ORACLE_HOME/scripts/susan.txt ]; then
echo "susan passed"
else
echo "susan failed"
exit 1
fi

if [ ! -s $ORACLE_HOME/scripts/sreeram.txt ]; then
echo "sreeram faile"
  exit 2
else
echo "sreeram passed"
exit 0
fi

OUTER SCRIPT:

/usr/oracle/scripts/t.sh
if [$? -eq 0]
then echo "success";
else
echo "failure";
fi


And I am getting the follwing message:

tis33:oracle:>z.sh
susan passed
sreeram passed
z.sh: [0: not found
failure
tis33:oracle:>


Thanks,


Sreeram.
ASKER CERTIFIED SOLUTION
Avatar of szetoa
szetoa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
if [ $? -eq 0 ]
Thank You.

Sreeram.
Ok, not to get all huffy over 2 points, but what was wrong with my original answer?

Yes, you can do things the way szetoa mentions, but I rejected these when composing my answer because
  /script1 && /script2

is _so_ much easier.
Hi Chris,

I am not clear about what you suggested, what happens when script1 fails to execute?

Thanks,

Sreeram.
If script1 fails (i.e., exits with a return code other than '0'), then script2 will not execute.  That's what the '&&' shell syntax means.

If you really need something fancier than this, then I suggest using a third script that looks something like

if /script1
then  script2
else  script3
fi
Hi Chris,

I am very sorry for rejecting your answer with out getting full clarification from you. I was of the view that all the scripts in the list will be executed one after the another no matter what is the out come of the previous. Thank You very much Chris for this clarification and for your suggestion.

Thank You,

Sreeram.