for loop not working on csh tcsh

bugzeo

Member

Reaction score: 18
Messages: 95

Using shell csh, which is tcsh 6.21.00:
foreach i ( * ) ; echo $i ; end
However asks:
foreach?

And waits for some input. Any help please?
Noticed that with sh works perfectly:
for i in * ; do echo $i ; done
 

jmos

Well-Known Member

Reaction score: 186
Messages: 254

";" is the problem - you will need to use more lines for your script:
Code:
#!/bin/tcsh
foreach VALUE (`some program`)
    echo $VALUE
end
 

ralphbsz

Son of Beastie

Reaction score: 2,301
Messages: 3,212

Noticed that with sh works perfectly:
General comment: (t)csh and (ba)sh are very different internally. The fact that simple things work the same misleads people into believing that they are compatible. They are not. My personal solution is to use exactly one shell, and about 20 years ago I standardized on bash. Others swear by (pd-) ksh or zsh, and several others. But pick one and get good at it.

Old joke: Beware or the man who has only one gun, because he knows how to use it.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,033
Messages: 38,480

tcsh(1) is great for interactive use, it's not that good to write scripts with. For scripts you should really use sh(1).
 
OP
B

bugzeo

Member

Reaction score: 18
Messages: 95

";" is the problem - you will need to use more lines for your script:
Code:
#!/bin/tcsh
foreach VALUE (`some program`)
    echo $VALUE
end
Yeah that works if you put that in a file. I guess you can't do it one-liner with tcsh.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,033
Messages: 38,480

Maybe not as a one-liner but still very usable interactively.

Code:
dice@molly:~ % foreach f (`seq 1 10`)
foreach? echo $f
foreach? end
1
2
3
4
5
6
7
8
9
10
 
Top