Jump to content

foreach loop


Recommended Posts

I'm busy revising for a test on shell basics tomorrow morning so a prompt reply would be appreciated.

 

I'm running through some excercises and while attempting to get a foreach loop running the shell seems to be telling me there is no such thing. csh keeps coming up foreach? while bash just doen't do anything at all. Comments please.

Link to comment
Share on other sites

OK some more information:

 

apparently there is no foreach in korn / bash only in the cshell

 

foreach? is the prompt to type the next instruction on typing end at the foreach? prompt the loop should execute.

 

Why it's not working I have no idea.

Link to comment
Share on other sites

To my knowledge bash has no 'foreach' command at all. But zsh does.

 

\$ foreach dir (*/)
foreach> echo $dir
foreach> end
Mail/
RCS/
bin/
etc/
evolution/
foo/
tmp/
var/

 

Ofcourse bash can do the same with its for builtin:

~$ for dir in */                     
> do echo $dir
> done
bin/
etc/
evolution/
foo/
Mail/
RCS/
tmp/
var/

 

Is that what are you looking for.

 

[Edited] I had to go in a hurry when I was writing this post so I half posted it. Now I've completed the bash part ;)

Edited by aru
Link to comment
Share on other sites

Theres a foreach in the c shell

I can now make it work at the prompt but it won't work, as yet, when written into a script. A little annoying but not the ned of the world it's only one excercies out of about 50.

 

ned ? I think I meant end but the ned of the world sounds pretty good! :D

Link to comment
Share on other sites

I've done the test and there was no foreach question on it.

 

Everything was OK but there was one question I couldn't do (posted it elswhere)

 

We were expected to use the cshell.

 

the syntax I have goes

foreach colour (red green blue yellow)

echo a colour $colour

end

 

You type the first statement at the $ prompt after you hit enter then each succeeding statement at a special foreach ? prompt

it executes at end <enter>

 

According to the student demonstrator you use the foreach in live terminal sessions not in scripts. He could be right. though that wasn't what the original tutorial sheet suggested.

 

Is there a good book on bash scripting? Web tutorials abound, but I'd like something that would lie on a desk.

 

 

 

thanks to all

 

Will

Link to comment
Share on other sites

You're wellcome

 

About Valencia, I'm quite happy, we are second, just one point behind real madrid even after the scandalous match between both teams (well exaclty Valencia versus real madrid + referee) :angry:

 

Amunt VALENCIA!!!! :headbang::jester:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...