Link to home
Start Free TrialLog in
Avatar of as93is
as93is

asked on

Combine shell scripts into one shell script and run combined shell script passing a parameter through CRON

Combine 3 shell scripts into one shell script and run combined shell script passing a parameter through CRON

I created three shell scripts.  One for export, one for append, and one to truncate -- export.sh, append.sh, truncate.sh.

I have to combine all the three shell scripts into a master shell script.  This master shell script should have a parameter and ORACLE_SID should be passed as the value for this parameter.

Only when the EXPORT script runs successfully, the append.sh script should run.  Otherwise, error code/message should come up and the master script should be exited.

Only when the APPEND script runs successfully, the truncate.sh script should run.  Otherwise, error code/message should come up and the master script should be exited.

When all the three shell scripts run successfully, the master script should be exited with a success return code.  Otherwise, an error code/message should come up.

CRON JOB TO RUN THE MASTER SHELL SCRIPT

The master script is run through a CRON job and the value of the ORACLE-SID should be passed to the master script when the cron job runs.

Please send me the code to meet the above requirements as there is a time constraint.

I would like award this question 1000 points because it is difficult.  This is an urgent task.

Thanks.
Avatar of sjm_ee
sjm_ee
Flag of United Kingdom of Great Britain and Northern Ireland image

For the first part, master.sh should contain something like this:

export.sh && append.sh && truncate.sh

The "&&" construct controls conditional execution.

The hard part is getting your parameter into a cron job. A cron job start without a controlling terminal and so you cannot prompt for input, without assuming that a user will be sitting at a particular terminal at the time the job runs. You could put the parameter in a known file, and then have the script read the file. Or you could have the parameter encoded in the cron entry and change it when necessary. Or you could create a non-recurring cron job with a particular value of the parameter by using the "at" command. But in general, parameter passing to a cron job is not a good practice.
Avatar of as93is
as93is

ASKER

I am a very new beginner to shell scripting and I have a deadline to meet.  I need help with the code.

The code could be something like the following:

combined_script.sh

#!/bin/ksh

ORACLE_SID=$1

export.sh $ORACLE_SID

if [ [ $? -ne 0 ] ]
then
        echo "Error"
fi

append.sh $ORACLE_SID

if [ [ $? -ne 0 ] ]
then
        echo "Error"
fi

truncate.sh $ORACLE_SID

if [ [ $? -ne 0 ] ]
then
        echo "Error"
fi

Then I have to run the combined.sh script through cron passing ORACLE_SID as the parameter.

Please send me the correct code for the above requirement.

Thanks.
Avatar of woolmilkporc

Hi again as93is,
your code is mostly correct but note the following
In the cron environment you must supply paths to your scripts, as you have only a rudimentary environment, so use e.g.
/aaa/bbb/export.sh
If you want the script to end in case one of the contained scripts fails, you must add an
exit 0
after your echo "Error" statements.
The cron entry is simple
00 1 * * * /aaa//bbb/combined.sh mySID >/dev/null 2>&1
What sjm_ee wrote is of course correct if you don't want to hardcode the SID into the crontab.
Let us know if this is the case.
wmp
 
Oh, a typo ....
of course there is only one '/' between 'aaa' and 'bbb'
(but this wouldn't have caused an error)
Avatar of as93is

ASKER

FINAL COMBINED SCRIPT

combined_script.sh

#!/bin/ksh

ORACLE_SID=$1

export.sh $ORACLE_SID

if [ [ $? != 0 ] ]
then
        echo "Error"
fi
exit 0

append.sh $ORACLE_SID

if [ [ $? != 0 ] ]
then
        echo "Error"
fi
exit 0

truncate.sh $ORACLE_SID

if [ [ $? != 0 ] ]
then
        echo "Error"
fi
exit 0

#Main

while getopts :d  OPT
do
   case $OPT in
     d | +d ) DBNAME=$OPTARG ;;
   esac
done

exit

==========================================================

FINAL CODE FOR

CRON ENTRY TO RUN combined_script.sh ON ALL DATABASES ON ONE LPAR BY GETTING ORACLE_SID FROM THE COMBINED.SH SCRIPT PARAMATER

00 1 * * * /aaa//bbb/combined.sh -d 1  >/dev/null 2>&1   1 is the database name or ORACLE_SID

00 2 * * * /aaa//bbb/combined.sh -d 2 >/dev/null 2>&1    2 is the database name or ORACLE_SID

00 3 * * * /aaa//bbb/combined.sh -d 3 >/dev/null 2>&1    3 is the database name or ORACLE_SID

WILL THE ABOVE COMBINED_SCRIPT.sh RUN THROUGH AT DIFFERENT TIMES FOR DIFFERENT ORACLE_SID AND RUN/EXECUTE EACH SHELL SCRIPT IN THE COMBINED.SH SCRIPT BY

PASSING THE PARAMETER ORACLE_SID?  PLEASE LET ME IF I HAVE UNDERSTOOD THE LOGIC CORRECTLY.


PLEASE GO TRHOUGH THE ABOVE TWO CODES AND CORRECT IT IF NECESSARY.

PLEASE LET ME KNOW IF THE ABOVE CODE FOR THE COMBINED_SCRIPT.SH SCRIPT AND THE CRON JOB WORK CORRECTLY.
WE DON'T WANT TO HARDCODE THE SID INTO THE CRONTAB.

PLEASE CHECK THE CODE FOR THE COMBINED_SCRIPT.SH AND CRON AND LET ME KNOW IF THE CODES ARE CORRECT.

THANKS.
Questions, questions, questions ...
 
First of all, the 'exit' must be placed before the 'fi', otherwise the script will always exit unconditionally!
 
Second, you can't intermix the getopt stuff and the method of using positionlal parameters like $1 ! In your code, $1 would carry the value '-d', which is probably not what you want.
So, either keep on using getopts and replace $1 with '$DBNAME'  and place the stuff under #MAIN where it belongs - at the beginning (!), or
omit the -d in crontab,  keep on using $1 and throw getopts away.
 
"WE DON'T WANT TO HARDCODE THE SID INTO THE CRONTAB"
Huh? What else did you do with '-d 1/2/3' ?
Avatar of as93is

ASKER

Again, thank you very much for your prompt feedback.

I am a beginner with shell scripting and I have to get this done today.

Here is my REVISED combine_script.sh:

combined_script.sh

#!/bin/ksh

ORACLE_SID=$1

export.sh $ORACLE_SID

if [ [ $? != 0 ] ]
then
echo "Error"
exit 0
fi


append.sh $ORACLE_SID

if [ [ $? != 0 ] ]
then
echo "Error"
exit 0
fi

truncate.sh $ORACLE_SID

if [ [ $? != 0 ] ]
then
echo "Error"
exit 0
fi


exit - SHOULD I KEEP THIS AT THE END OF THE SCRIPT

==========================================================

REVISED CODE FOR CRONTAB ENTRY

CRONTAB ENTRY TO RUN combined_script.sh ON ALL DATABASES ON ONE LPAR BY GETTING ORACLE_SID FROM THE COMBINED.SH SCRIPT PARAMATER

00 1 * * * /aaa//bbb/combined.sh $1 >/dev/null 2>&1

OR

00 1 * * * /aaa//bbb/combined.sh MYSID >/dev/null 2>&1


WHICH OF THE ABOVE CRONTAB ENTRY WILL WORK? HOW WILL CRONTAB ENTRY GET MYSID VALUE OR $1 VALUE? THIS IS NOT CLEAR TO ME.

PLEASE LET ME KNOW IF THE ABOVE CODE FOR THE COMBINED_SCRIPT.SH SCRIPT AND THE CRON JOB ARE CORRECT.

THANKS.
exit at the end of a script is optional, but it's good custom to have it there. With 'exit 0' you take care that the script will exit with a returncode of 0.

Now, to make it clear:
$1 in a ksh script gets replaced by the first string you type at the commandline after the script's name.
So, when you issue 'combined.sh MYSID', the variable $1 will have the value 'MYSID', and only and exactly 'MYSID' and nothing else.

Write a little script 'parms_test', containing:
#!/bin/ksh
echo $1
exit
make it executable (chmod +x parms_test) and start it by typing at the commandline 'parms_test "Hello World!"'
The result will be
Hello World!
and nothing else.
This works with more strings the same way:
Script contains 'echo $1 $2 $3',
you type
'parms_test "Hello World!" "I am" "here!"'
and the result will be
Hello World! I am here!

So the second crontab entry above would be correct, given your SID were really 'MYSID' or you replaced 'MYSID' with the correct, real SID of your DB.

And this means, btw, with the above method you will have your SID 'hardcoded' in the crontab.

wmp


Avatar of as93is

ASKER

Please clarify:
if [ [ $? != 0 ] ]
then
echo "Error"
exit 0                        ---  Should I have exit 0 or just exit here
fi
 CRONTAB ENTRIES
1.

For crontab entry below, I substitute  MYSID with abc when I make an entry in the crontab.  Is this correct?
00 1 * * * /aaa//bbb/combined.sh MYSID >/dev/null 2>&1
2.

Why will 00 1 * * * /aaa//bbb/combined.sh $1 >/dev/null 2>&1 FAIL?

It is not clear to me how the parameter ORACLE_SID=$1 in the combined_script.sh works in the CRONTAB entry.  Please clarify.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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 as93is

ASKER

Your detailed explanation has made it very clear to me why the parameter $1 will not work in the crontab entry.
This why you said that mysid with the real value is hardcoded in the crontab entry.
When I give the real value for mysid, this will make both the cron job run and also execcute the export.sh script which is in the combined_script.sh.
Now it is all clear to me.
I'm thankful to you for your detailed clarifications and PROMPT FEEDBACK.
THANKS!!!
Avatar of as93is

ASKER

woolmilkporc
Thank you very much for your excellent, timely solution to my question.  Also, you have answered all my questions.
Thanks!!!
MODERATOR:

I request you to assign  woolmilkporc 2000 points for his excellent, timely solution to my question.

Thanks!!!
You're always welcome!
Have much fun and success!

Norbert