How to count using foreach

I have a simple csh script that has a simple foreach loop that goes over numbers, from 1 to 10:

foreach n(1 2 3 4 5 6 7 8 9 10)
...
end

Now I want to expand the script to work on over a hundred consecutive n values. Obviously, typing all the numbers between 1 to 100 is an unreasonable solution. I think I saw once a way to count with a foreach loop by specifying the range, or something like that. I can't find it now, and when searching forums and google, I can't find a simple solution for that.

Is it possible to make foreach to go over values without explicitly typing them? Specifically, can I use foreach to count from 1 to 100 without entering all the numbers myslef?

Alternatively, I guess I could use a while loop, but at this point I am curious if foreach can do that too.

Thanks for any help.

if you have jot available:

% foreach n ( `jot 10` )
foreach? echo $n
foreach? end
1
2
3
4
5
6
7
8
9
10

If jot is not available, perhaps you have seq, which serves a similar function.

I don't have jot apparently (I get jot: Command not found). I may have seq cause the error is gone. which seq gives /usr/bin/seq

I replaced jot with seq in your script, but I am getting:
foreach?: No match.
foreach?: No match.

Am I missing something? I am a fairly beginner so it may be something obvious?

Thanks!

While you're still an beginner, swich shells. See Csh Programming Considered Harmful and Top Ten Reasons not to use the C shell

I don't know, mcbenus. I do not have any experience with any variants of the csh. For reasons why, see pludi's links. :slight_smile:

Best of luck,
alister

Can you paste the loop which gave the above error

i=1
while [ $i -lt 100 ];do
echo $i
i=$(($i+1))
done

Hi.

Most people recommend against csh family for scripting, but if you had to use it for some reason:

#!/usr/bin/env tcsh

# @(#) s2	Demonstrate numeric sequence, seq, [t]csh.

echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" tcsh
echo

set my_list = ( `seq 10` )

echo " my_list is $my_list"

echo
foreach item ( $my_list )
	echo $item
end

exit 0

producing:

% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
tcsh 6.14.00

 my_list is 1 2 3 4 5 6 7 8 9 10

1
2
3
4
5
6
7
8
9
10

Best wishes ... cheers, drl

-----

Standard advice: use Bourne shell family for scripting.