LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-10-2008, 04:41 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Rep: Reputation: 39
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.

Code:
#!/bin/bash

  test1() {
    echo test11
  }
echo free1
  test2() {
    echo test22
  }
echo free2

test1
echo '--'
test2
echo '--'
test1
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.
 
Old 01-10-2008, 04:48 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by helptonewbie View Post

I hope all that makes sense,
nope, sorry!

is this problem generic or specific?
 
Old 01-10-2008, 04:51 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39

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.
 
Old 01-10-2008, 05:08 PM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,203

Rep: Reputation: 292Reputation: 292Reputation: 292
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
}

Last edited by dive; 01-10-2008 at 05:16 PM.
 
Old 01-10-2008, 05:13 PM   #5
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
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
 
Old 01-10-2008, 05:25 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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?)

I use a makefile.
 
Old 01-10-2008, 05:45 PM   #7
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517

Original Poster
Rep: Reputation: 39
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
 
Old 01-10-2008, 06:12 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by helptonewbie View Post
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
use a while/for loop coupled with if/else to test for your condtion, and then break/continue for the actions.
 
Old 01-10-2008, 06:26 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by helptonewbie View Post
The only time i know of make files is when installing from source and you use a make file to make install etc etc.
well, yes and no, you can use make for whatever you want.

if you have a series of steps, and each depends on another, you can touch files
and run any set of commands you want and let make work it out.

if you have 5 steps and 3 fails, when you next run make will start at 3.

see what i mean?
 
Old 01-10-2008, 06:34 PM   #10
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
There's a goto statement is the C shell...
Maybe, that's what you saw? There's no such thing in bash or sh, for sure...
 
Old 01-10-2008, 11:10 PM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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...
 
Old 01-11-2008, 05:49 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
you should really tell us what you need doing, rather than how you think it should be done
 
  


Reply


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
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 05:18 AM
using cron to start and stop a bash script monty Linux - Software 2 03-20-2006 02:30 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


All times are GMT -5. The time now is 09:45 PM.

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