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
sreeramreddyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chris_calabreseCommented:
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
0
szetoaCommented:
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.
0
sreeramreddyAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

sreeramreddyAuthor Commented:
I tried the second sample, but I am not able to capture the outcome of the script. It is giving a message variable syntax.
0
szetoaCommented:
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)
0
szetoaCommented:
Can you show how to exit your first script?  If you use exit 0, the second script should capture the return code.
0
sreeramreddyAuthor Commented:
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.
0
szetoaCommented:
It's been a long time since I lost the possession of a UNIX box.   Try to put a blank between '[' and "$?" and '0' and ']'.  You are getting close.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
if [ $? -eq 0 ]
0
sreeramreddyAuthor Commented:
Thank You.

Sreeram.
0
chris_calabreseCommented:
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.
0
sreeramreddyAuthor Commented:
Hi Chris,

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

Thanks,

Sreeram.
0
chris_calabreseCommented:
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
0
sreeramreddyAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.