Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'csh version' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
csh version
Authored by: sven on May 10, '01 04:56:06AM

Ok, let's start the big "Here's my way" contest. Let me submit the csh version with "foreach":

[Galileo:~/Temp] sas% touch 1.jpg
[Galileo:~/Temp] sas% touch 2.jpg
[Galileo:~/Temp] sas% touch 3.jpg
[Galileo:~/Temp] sas% foreach f ( *.jpg )
foreach -> set base = `basename $f .jpg`
foreach -> mv $base.jpg $base.gif
foreach -> end
[Galileo:~/Temp] sas% dir *.gif *.jpg
-rw-r--r-- 1 sas staff 0 May 10 10:14 1.gif
-rw-r--r-- 1 sas staff 0 May 10 10:14 2.gif
-rw-r--r-- 1 sas staff 0 May 10 10:14 3.gif

Note that the "foreach ->" prefix is printed by the shell after you hit return at the end of the "foreach" line. You get out of this with "end" on a line by itself. That's when the loop is actually executed.

I think this approach is a little more transparent than the awk/sed thing, especially since according to Murphy you'll never remember the exact syntax when you need it. And of course you won't have the time to read the man pages or they won't be installed or ... well, you know, that kind of thing...
foreach is really great when you don't have to modify the items like in the example above, for example if you want to batch run a program that doesn't take multiple files as input. E.g.:
[Galileo:~/Temp] sas% foreach f ( /Users/sas/some_files* )
foreach -> some_prog $f
foreach -> end

The drawback is that you can't use the history to get back what you typed inside the loop.

Sven



[ Reply to This | # ]