LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-15-2010, 09:03 AM   #1
liny1984
LQ Newbie
 
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23

Rep: Reputation: 0
Question Start script from middle


Hello everyone,

I have a large script which calls up some command line tools of Xilinx ISE(hardware synthesis tools vendor)

Any one has an idea how to restart a script from the middle ?

The commands in the script have to be executed in sequence, however there can be errors at any step. I cant use functions for each section as all the steps have to execute one after the other. I might break the steps up into functions
such as :

a()
b()
c()
d()

and if it fails in c(), then I comment out a() & b() , so it starts from
c(). But i had rather not play around with the original script code.

Is there a simpler way ? I am hoping for something that can be told in the first line of the script or controlled through the command line invocation such as
> sh -line 10 pr.sh

to start from line 10 of the script.

Thanks
 
Old 11-15-2010, 09:16 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,641

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Well I am not sure I completely understand what you want, but if you did break it into functions (which of course only run when called) you could pass the function names
on the command line and parse them so they run in the desired order.

If you meant something completely differently I would need some more clarification?
 
Old 11-15-2010, 09:27 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
How about a script that's laid out like (not tested)
Code:
function a definition
function b definition
function c definition
function d definition

case $1 in 
    a )
        call function a
        ;&
    b )
        call function b
        ;&
    c )
        call function c
        ;&
    d )
        call function d
esac
 
Old 11-15-2010, 09:35 AM   #4
liny1984
LQ Newbie
 
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23

Original Poster
Rep: Reputation: 0
Smile

Thanks for the replies !

Catkin,
if I need to call all the functions after the failed function. So if function b() failed I would need to call b,c,d in that order. I could do it like :

Code:
function a definition
function b definition
function c definition
function d definition

case $1 in 
    a )
        call function a
        call function b
        call function c
        call function d
        ;&
    b )
        call function b
        call function c
        call function d
        ;&
    c )
        call function c
        call function d
        ;&
    d )
        call function d
esac

The above obvious solution may work for 4 functions. But what if I have 10 or more ?

grail,
Yes but as I said the number of functions are quite large as the failure sections are quite small - what I mean is there are single commands which can return non zero.Each of them consume a considerable amount of time. There are a large number of such commands - about 30.
If I made them all into functions and gave a list in the command line I might as well rewrite the script

One possible solution is to run the script through another wrapper script. The wrapper accepts 2 parameters : the script to run and the line number from where to run it:
> sh wrapper.sh myscript.sh 10

Now it copies out the lines from 10 to the last line of myscript.sh to temp.sh and then executes temp.sh instead. Since my script is a sequence of commands without any flow control structures, it may work.

Last edited by liny1984; 11-15-2010 at 09:43 AM.
 
Old 11-15-2010, 09:57 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,641

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Quote:
So if function b() failed I would need to call b,c,d in that order
I am not sure this makes any sense?

If b() fails you want to call b() again???
Why would it not fail on being called a second time?

Generally when one has failures in a script/program then these need to be sorted out so as to not error?

Quote:
Since my script is a sequence of commands without any flow control structures,
This in itself would seem to be a problem to me seeing you are trying to fix the errors by skipping or repeating steps.
ie. you are trying to implement flow control in a very round about way.
 
Old 11-15-2010, 10:06 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by liny1984 View Post
if I need to call all the functions after the failed function. So if function b() failed I would need to call b,c,d in that order.
Understood and I believe the code suggested will do it. Sorry for not explaining about the ;& where we normally use ;;. Quoting from here: "Using ;& in place of ;; causes execution to continue with the command-list associated with the next clause, if any. Using ;;& in place of ;; causes the shell to test the patterns in the next clause, if any, and execute any associated command-list on a successful match". So ;& simply drops through into the next case code without testing its match.
 
1 members found this post helpful.
Old 11-15-2010, 10:17 AM   #7
liny1984
LQ Newbie
 
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23

Original Poster
Rep: Reputation: 0
grail,

It does make sense. b() might fail due to an issue with a file. That file may need to be changed and then b() will run correctly. as an eg. b() might contain only this command


ngdbuild top.ngc

top.ngc is created by an external tool and placed in the current directory. The above program tries to read top.ngc, does its own processing and converts it into the top.ngd file. However while processing it may determine that top.ngc is not in the right format so it aborts with return code != 0. So then I go run the external tool again to fix the top.ngc file, copy it to the current directory and run b() again. This time b() will succeed.

Now as for the absence of flow control, I do not need it to execute a list of commands as I do not have any conditions or menu in the beginning. if there is an error, only the failed commands and all after it needs to be executed so the steps are comple. its a straight forward flow in only one direction top >>>> to >>>> bottom

Last edited by liny1984; 11-15-2010 at 11:33 AM.
 
Old 11-15-2010, 06:17 PM   #8
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,916

Rep: Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777
If there are blocks, whether they are functions or not, you just don't want to run them again, once they have run successfully?

Why don't you just write out a file, say a.ok when a has completed successfully? And then test for the prescence of the a.ok, b.ok, etc, etc?
 
  


Reply

Tags
bash, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
start script bash when system start,stop,reeboot, how ?? melmar Linux - General 4 12-10-2009 07:58 AM
script to add text in middle of file Plato Linux - Newbie 3 07-22-2009 06:39 PM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 05:50 PM
cron stops in the middle of the script djzanni Linux - Newbie 6 06-02-2005 06:29 PM
How to start a Tcl/Tk script by simply invoking the script file itself ? cyu021 Programming 2 10-10-2004 12:00 PM


All times are GMT -5. The time now is 02:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration