Get script to start again at a certain point in the script (bash)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Get script to start again at a certain point in the script (bash)
Hi All,
I'm sure this is possible or i've seen it happen somewhere but can't remember anything about it basically what i would like is stuff runs in a script in order as each command executes down the script. What i'd like is a load of commands have executed but a little further down a parameter lets say is then tested and is wrong, i'd like the script to then be able to start again from a point i specify in the script. I know i can get something similar to happen with functions but it doesn't quite work how i want.
So whereas with using a function the script above output is:-
free1
free2
test11
--
test22
--
test11
i'd like to know how i can get something similar but the output would be when running the script:-
free1
free2
test11
free1
free2
--
test22
free2
--
test11
free1
free2
I hope all that makes sense, i know that looks like it would create a loop but i haven't shown that the output of re-running the script would create a different output and so it would skip past the test1 again so to speak once the correct output is got. In other words it would include a few more if statements and stuff, i'm sure its possible somehow just can't damn remember???
Cheers
Mark
Last edited by helptonewbie; 01-10-2008 at 04:48 PM.
Typical there's always one, na, sorry i didn't know how best to explain it here goes again
Basically, when you run a script (./scriptname) it sorta runs the commands in order down the file, so ..
command1
command2
command3
what i want to be able to do is that but also:-
command1
command2
command3
command4
command5
command6 --> go back and start at run command2 again at which point not only will it run command2 again but all the others from that point, command2,command3,command4,command5,command6,command7.... and so on
command7
command8
command9
Last edited by helptonewbie; 01-10-2008 at 05:09 PM.
Unfortunately bash doesn't support goto which would make life so much easier. All asm is compiled into a bunch of gotos anyway so I don't see the point of not using it...
You could use case or simple tests to do what you need but it will be a pain and probably involve about 10x more tests than you will like. Have a look here at branches: http://www.tldp.org/LDP/abs/html/loops.html
Hmm just thinking, maybe you could make a script that calls itself with a switch:
Code:
if [[ $1 -ge 1 ]]; then function_1; fi
if [[ $1 -ge 2 ]]; then function_2; fi
...
...
function_1(){
if [[ test == value ]]; then thisscript 1; fi
do more stuff
}
Na, thats not quite it, thanks for replying. See the whole point is that is goes back and re-runs all this script again otherwise its like rewriting it all again and again. Yeah i guess i could put the thing into a for loop, it might be what i need. Hmm i'll try and think of another way to tackle it, thanks and yes it would have been simpler i was sure i've seen it done and in bash to, maybe i am just getting confussed with it and what i saw before was the way it can call back to a function by name, but this of course doesn't run all the script again once the function has closed it just carries off from the point where the script left off so to speak, hey probably confusing the matter again i should stop talking. Cheers for the help
well, when I've done processing on large blocks of data with lots of
intermediate stages and don't
want to restart from the beginning (is this what you want?)
Thanks dive for the edit, but no that's not what i want i understand that stuff already thanks anyway for your time and replies. and bigears ( no pun intended ) I'm not entirely sure what you are suggesting. The only time i know of make files is when installing from source and you use a make file to make install etc etc. If that's the case of what you are talking about then i don't think thats what I'm looking for. However i must say that is something I'm interested in knowing how that all works and how to do all that stuff but i wouldn't know where to start with it.
Cheers again people but maybe the best way forward is a for loop or maybe a 'while' loop would be better actually. Hmm never mind thanks peeps
It sounds like you want to mess around with the 'continue' cmd: http://tldp.org/LDP/abs/html/loopcontrol.html
Alternatively, I'd put all the real work in fns and call them as needed...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.