Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.