• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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
0
sreeramreddy
Asked:
sreeramreddy
  • 6
  • 4
  • 3
  • +1
1 Solution
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now