How to count using foreach


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count using foreach
# 1  
Old 01-03-2010
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.
# 2  
Old 01-03-2010
if you have jot available:

Code:
% 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.

Last edited by alister; 01-03-2010 at 04:04 PM.. Reason: Add mention of seq
# 3  
Old 01-03-2010
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!
# 4  
Old 01-03-2010
While you're still an beginner, swich shells. See Csh Programming Considered Harmful and Top Ten Reasons not to use the C shell
# 5  
Old 01-03-2010
Quote:
Originally Posted by mcbenus
Am I missing something? I am a fairly beginner so it may be something obvious?
I don't know, mcbenus. I do not have any experience with any variants of the csh. For reasons why, see pludi's links. Smilie

Best of luck,
alister

Last edited by alister; 01-03-2010 at 06:13 PM.. Reason: fix typo/grammar
# 6  
Old 01-03-2010
Quote:
Originally Posted by mcbenus
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!
Can you paste the loop which gave the above error
# 7  
Old 01-03-2010
Code:
i=1
while [ $i -lt 100 ];do
echo $i
i=$(($i+1))
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach issue

Hello, I found that this foreach should work with two lists (source: Wikipedia.org) foreach i {1 2 3} j {a b c} { puts "$i $j"} == I try smth. like: With two text files: first.part second.part foreach first (`cat first.part`) second (`cat second.part`) toolcommand $first... (22 Replies)
Discussion started by: unknown7
22 Replies

2. Shell Programming and Scripting

Using foreach with two lists

Hi everybody, I'm trying to use a foreach command with two lists. The file.txt looks like this: var1: 100 200 300 var2: 3 6 9 I'm trying to use a foreach command to associate the two variables together. My script looks like this: #! /bin/tcsh set a=(`cat file.txt | grep 'var1' | cut -d... (8 Replies)
Discussion started by: SimonWhite
8 Replies

3. Shell Programming and Scripting

Perl - Sort and Count foreach line

Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically. Here is what i did ... I can print all the lines but struck to sort each line some were: I am getting the output as: I... (5 Replies)
Discussion started by: sureshcisco
5 Replies

4. Shell Programming and Scripting

Learning foreach

im newbie at shell scripting. why do the following code #!/bin/tcsh setenv CBC ~/cbc/models/ foreach mix (p00p00 p02p00 p02p04) echo $mix cp $CBC/*$mix*Gyr*fits $mix/ end print(copy) only the first mix? % ./copyfromcbc.sh p00p00 wasn't it supposed to run through all words... (0 Replies)
Discussion started by: prtc
0 Replies

5. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

6. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

7. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies

8. Shell Programming and Scripting

foreach folder

Hi, I'm having a small issue here and I can't get it to work. I'm programming a script for bash and I need to do something to all the folder in a directory. So I'm in the directory and I want to use the foreach statement but I dont know how to reference all the folders of that directory. To make... (7 Replies)
Discussion started by: eltinator
7 Replies

9. Shell Programming and Scripting

foreach/grep help!

#!/bin/bash foreach x (67402996 67402998) { grep -a x FINAL2006.dat >> MISSING_RECORDS.dat } I'm trying to pass a list to the variable x, and then grep for that string in FINAL2006.dat... Final2006.dat is in the same folder as my .sh file. I call this with a .cmd file... At any rate,... (6 Replies)
Discussion started by: JimWork
6 Replies
Login or Register to Ask a Question