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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-15-2010, 08:03 AM
|
#1
|
|
LQ Newbie
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23
Rep:
|
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
|
|
|
|
11-15-2010, 08:16 AM
|
#2
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,383
|
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?
|
|
|
|
11-15-2010, 08:27 AM
|
#3
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
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
|
|
|
|
11-15-2010, 08:35 AM
|
#4
|
|
LQ Newbie
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23
Original Poster
Rep:
|
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 08:43 AM.
|
|
|
|
11-15-2010, 08:57 AM
|
#5
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,383
|
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.
|
|
|
|
11-15-2010, 09:06 AM
|
#6
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by liny1984
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.
|
11-15-2010, 09:17 AM
|
#7
|
|
LQ Newbie
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23
Original Poster
Rep:
|
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 10:33 AM.
|
|
|
|
11-15-2010, 05:17 PM
|
#8
|
|
Senior Member
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,692
|
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?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:07 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|