PDA

View Full Version : [SOLVED] BASH: something similar to a C "goto" command?



ryanVickers
September 29th, 2007, 10:28 PM
I'm looking for something that does the same thing as "goto" in C(++) for BASH. I know it's primitive, but... :p

ryanVickers
September 29th, 2007, 11:41 PM
I know theres nothing that comes with BASH, but if there's an add-on program in the repositories, or anything! [-o<

Ramses de Norre
September 29th, 2007, 11:50 PM
Using goto statements is considered bad practice and goto isn't being implemented in modern languages anymore (except some). You should use the more advanced control structures (while, if, for, case, ...).

1337455 10534
September 29th, 2007, 11:52 PM
Unless you gotsta use bash, I suggest using the os mod in Python and using while loops :lolflag:. Worked for me!
- Hey, thankx a lot for all the help with my gnome/terminal thread!

ryanVickers
September 29th, 2007, 11:53 PM
I'll try, but it won't be pretty! :p

In this case, a goto would be really useful, and quite harmless, but I can see why *they* would want to discourage use of them :p

1337455 10534
September 29th, 2007, 11:57 PM
Are you trying to use it for the delete useless files script? Yes, then, a goto would be benign.
Are there no loop funcs in BASH? weird...

ryanVickers
September 29th, 2007, 11:59 PM
Are you trying to use it for the delete useless files script? Yes, then, a goto would be benign.
Are there no loop funcs in BASH? weird...

no, no no! that's long done and working :p

there are loops in bash, but in this particular case, it's very complicated and would be a pin to use them :)

ryanVickers
September 30th, 2007, 12:03 AM
ok, how do you check if a file does not exist :p; I need the opposite of this :):
if [ -e $where ]
then
blah blah blah
....

walkerk
September 30th, 2007, 12:09 AM
perhaps (i haven't tested this)

if [ ! -e $where ]; then
echo "Not there guy!"
fi


look (http://www.gentoo.org/doc/en/articles/bash-by-example-p3.xml)

ryanVickers
September 30th, 2007, 12:13 AM
seems to work, but hold on everyone, I'll be a while before I get this working...

ryanVickers
September 30th, 2007, 12:15 AM
can while loops have else? :)

Wybiral
September 30th, 2007, 12:26 AM
can while loops have else? :)

Everything after the while loop?

ryanVickers
September 30th, 2007, 12:30 AM
right! :p

oh, dear, I'm getting tired. Well, I managed to *finish* it at least - just one more feature...

HermanAB
September 30th, 2007, 12:44 AM
Hmm, you should consider making it a Perl script instead. Perl is easier to code than Bash and much more powerful. (and no, Perl doesn't *have* to look like line noise).

ryanVickers
September 30th, 2007, 12:47 AM
ok, thanks for all the help everyone, and sorry to anyone else reading this who needs a goto command :p

You can all check out the new version of "mkrar", number 1.7! It is now even more safe and adapting!; it will detect if a file/folder you want to rchive/go into exists or not, and if it doesn't, it will tell you and let you try again to type a correct path in! Also, it already assumed the home directory for the path to the file, and now it will automatically assume the folder/file to archive to be the first item in the folder!!! check it out!

1337455 10534
September 30th, 2007, 12:49 AM
I would declare this thread as solved...
Ryan, if you'd do the honors?...

ryanVickers
September 30th, 2007, 12:49 AM
Hmm, you should consider making it a Perl script instead. Perl is easier to code than Bash and much more powerful. (and no, Perl doesn't *have* to look like line noise).



Yeah, I've heard good things about it, but I don't really want to learn another language! :p I already know actionscript 1 and 2, a tiny bit of C, (I can read better than wrote :)), and now I'm learning bash, but I think that would just be too much! :p Unless it's really easy...

ryanVickers
September 30th, 2007, 12:50 AM
I would declare this thread as solved...
Ryan, if you'd do the honors?...

yes, indubitably! :)

ghostdog74
September 30th, 2007, 03:27 AM
I'm looking for something that does the same thing as "goto" in C(++) for BASH. I know it's primitive, but... :p
comes with csh though.

ryanVickers
September 30th, 2007, 03:29 AM
Does that include all the same features as bash? (for other users - I"m done here :p)

ghostdog74
September 30th, 2007, 04:26 AM
Does that include all the same features as bash? (for other users - I"m done here :p)
I randomly picked a site here (http://elqui.dcsc.utfsm.cl/util/unix/UnixIntro/Scripts.html). you can take a look at how gotos are done in csh. you can also read this (http://www.tac.nyc.ny.us/mirrors/tcsh-book/csh-whynot). by the way, i am just giving information and not advocating the use of csh just because it has goto.

engla
September 30th, 2007, 12:24 PM
read the man page for 'test'; strangely, test and [ are synonyms.

Can't you use functions instead of a goto?

mssever
September 30th, 2007, 04:37 PM
Hmm, you should consider making it a Perl script instead. Perl is easier to code than Bash and much more powerful. (and no, Perl doesn't *have* to look like line noise).
Ruby is a more modern language that includes many of Perl's features. I used to do a lot of bash scripting. However, since I've learned Ruby, I've found that I much prefer Ruby over bash. Yes, there's some learning involved, but you don't have to deal with bash's syntax and quoting oddities. And, in ruby you can run any shell command any time you want, if you find that easier for some particular task. Plus, Ruby is much more readable than even well-written Perl code.

comes with csh though.
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

ghostdog74
October 1st, 2007, 02:05 AM
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/.
read the second link of of my post.