Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run the Script from particular point ... 1

Status
Not open for further replies.

h3nd

Programmer
Jul 1, 2006
147
AU
Hi guys,

I need help to run my script from particular point,

at the moment my script contain children scripts and looks like this :
Code:
#!/bin/ksh

Script1
if[ $return_code -ne 0 ];then
exit $return_code

Script2
if[ $return_code -ne 0 ];then
exit $return_code

Script3
if[ $return_code -ne 0 ];then
exit $return_code

Script4
if[ $return_code -ne 0 ];then
exit $return_code

Script5
if[ $return_code -ne 0 ];then
exit $return_code

If I have to run the parent script, it took around 1 hour.

So I want if the one of the children script failed, I want to run from that script failed and then continue the rest of them.

Any idea how to do this ?

Thx guys
 
How about putting command line arguments into your script.

What i mean is at the begining have a bit that tests for an argument and if it finds one it understands then it skips the parts of the script that it doesn't need to run, but if not it just runs the whole thing.

Something like:
Code:
#!/bin/ksh

DO1=1
DO2=1
DO3=1
DO4=1
DO5=1

if [ $# -ne 0 ]; then
    if [ $1 == "SCRIPT2" ]; then
         then DO1=0 
    fi
    if [ $1 == "SCRIPT3" ]; then
         then DO1=0 
         then DO2=0
    fi
    if [ $1 == "SCRIPT4" ]; then 
          DO1=0 
          DO2=0
          DO3=0
    fi
    if [ $1 == "SCRIPT5" ]; then 
          DO1=0 
          DO2=0
          DO3=0
          DO4=0
    fi
fi
if [ $DO1 -ne 0 ]; then
   Script1
   if[ $return_code -ne 0 ];then
   exit $return_code
fi
if [ $DO2 -ne 0 ]; then
   Script2
   if[ $return_code -ne 0 ];then
   exit $return_code
fi
if [ $DO3 -ne 0 ]; then
   Script3
   if[ $return_code -ne 0 ];then
   exit $return_code
fi
if [ $DO4 -ne 0 ]; then
   Script4
   if[ $return_code -ne 0 ];then
   exit $return_code
fi
if [ $DO5 -ne 0 ]; then
   Script5
   if[ $return_code -ne 0 ];then
   exit $return_code
fi

I'm sure there are better ways of doing this but I hope it gives you the idea.
M

--------------------------------------
For animals, the entire universe has been neatly divided into things to (a) mate with, (b) eat, (c) run away from, and (d) rocks.
-- Terry Pratchett, Equal Rites
 
What about keeping a "state" file or something like that?


At the beginning of your parent script would read in the value from the state file, and if no value is stored in the state file (or the state file doesn't exist) write out to your state file a value of "1".

When the first child script completes successfully, the parent script writes out a value of "2" to the state file.

etc...

Surround each call to a child script that checks to see if the "state value" variable is less than or equal to the number of the script.

So maybe something like this:
Code:
#!/bin/ksh
statefile=/path/to/your/statefile
state=$(cat $statefile 2>/dev/null)
if [[ -z $state ]]
then
        state=1
fi

if (( $state <= 1 ))
then
        echo Starting script 1
        #script1
        if (( $? == 0 ))
        then
                state=2
        else
                exit
        fi
fi

if (( $state <= 2 ))
then
        echo Starting script 2
        #script2
        if (( $? == 0 ))
        then
                state=3
        else
                exit
        fi
fi

#... same for script 3 and 4 ...

if (( $state <= 5 ))
then
        echo Starting script 5
        #script5
        if (( $? == 0 ))
        then
                rm -ef $statefile
        else
                exit
        fi
fi

echo $0 completed

Maybe that's not exactly what you need?
 
Hi Brian,

I like your idea, I will try that.

Thanks man.
 
Good deal, and thanks for the star!

I left one important piece out (you probably saw it too), but after the state variable is changed (the lines that say state=2, state=3, etc.), you will need to echo $state > $statefile so that if the script exits and is restarted, it will have the last successful state in the state file.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top