Solved

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

Posted on 2008-10-28
13
689 Views
Last Modified: 2013-11-17
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.
0
Comment
Question by:as93is
  • 6
  • 6
13 Comments
 
LVL 14

Expert Comment

by:sjm_ee
ID: 22829294
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.
0
 

Author Comment

by:as93is
ID: 22829970
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22830330

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
 
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22830360
Oh, a typo ....
of course there is only one '/' between 'aaa' and 'bbb'
(but this wouldn't have caused an error)
0
 

Author Comment

by:as93is
ID: 22832620
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22832953
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' ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:as93is
ID: 22833198
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22834113
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


0
 

Author Comment

by:as93is
ID: 22834858
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.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 22835187
exit/exit 0:
Since you want to run your script under cron, and since  for error analysis you probably have a log (at least in export.sh you do have one, as I know), you should have  exit 0 to avoid cron's errorcode-driven emails.

MYSID:
If abc is your actual SID, then you are right.

combined.sh $1, why will this fail:
We're talking about an entry in crontab, which gets processed by the cron daemon, and cron doesn't know anything about $1. So $1 will be left empty. Your combined_script will thus be called with this empty string as first parameter, which means ORACLE_SID will be empty, because of ORACLE_SID=$1. Likewise, your export.sh will be called inside combined_script with this empty string as a parameter. So export.sh will not populate ORACLE_SID (here too we have ORACLE_SID=$1), and finally exp will not know which DB to process (because of the empty ORACLE_SID) and will thus fail.

wmp
0
 

Author Comment

by:as93is
ID: 22835883
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!!!
0
 

Author Closing Comment

by:as93is
ID: 31511043
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!!!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22888435
You're always welcome!
Have much fun and success!

Norbert

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now