11

In CSH foreach loop or for loop, how can I add a loop iterator or counter which increases from 10 to 1000 with steps of 20?

Something like foreach i (1..20..5) or for (i=1;i<20;i++).

skaffman
  • 398,947
  • 96
  • 818
  • 769
SkypeMeSM
  • 3,197
  • 8
  • 43
  • 61

3 Answers3

14

If you have the seq command, you can use:

foreach i (`seq 1 5 20`)
  ... body ...
end

If you don't have seq, here is a version based on @csj's answer:

@ i = 1
while ($i <= 20)
  ... body ...
  @ i += 5
end
Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
2

Any documentation I've found online seems to indictate no for loop is available. However, the while loop can be used. I don't actually know csh, so the following is approximate based on what I read:

set i = 10
while ($i <= 1000)
    # commands...
    set i = $i + 20
end
csj
  • 21,818
  • 2
  • 20
  • 26
  • 2
    Actually, it'd be `@ i = 1` and `@ i = $i + 1` – Dennis Williamson Feb 22 '11 at 20:17
  • @Dennis Thanks for the syntax correction. As for the values, the question requests a counter that goes from 10 to 1000 in steps of 20. Thus, an initial value of 10, and + 20 each time through the loop. – csj Feb 23 '11 at 04:26
  • The csh for loop is called with foreach. In fact, if you type "csh for" in the google search box, it shows foreach as the first option. – shellter Mar 07 '11 at 20:31
0

Or you can use expr. the following worked for me (in tcsh but csh should be the same):

% set n=0
% foreach x (`ls $A*`)
foreach? set n=`expr $n + 1`
foreach? echo $n
foreach? end

output is 1 2 3 4 etc

Tim V
  • 1,255
  • 1
  • 9
  • 5