# foreach works once only! C-Shell



## Ajax (Apr 19, 2012)

The next simple script shows only

```
0 - 7
```
while I would expect it to crawl through all three numbers saved in array. Am I doing something wrong?

```
#!/bin/csh                                                                                                              
                                                                                                                        
set servers = (7 25 160)                                                                                                
set i = 0                                                                                                               
                                                                                                                        
foreach server ($servers)                                                                                               
    echo "$i - $server"                                                                                                 
    set i=$i+1                                                                                                          
end
```


```
[root@FLOCK ~]# /bin/csh --version
tcsh 6.17.00 (Astron) 2009-07-10 (i386-intel-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```


----------



## fluca1978 (Apr 19, 2012)

It works straight on my system, are you sure there is nothing extra that causes a fail?


```
~> csh test.csh
0 - 7
0+1 - 25
0+1+1 - 160
~> csh --version
tcsh 6.17.00 (Astron) 2009-07-10 (i386-intel-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```


----------



## Ajax (Apr 19, 2012)

fluca1978 said:
			
		

> It works straight on my system, are you sure there is nothing extra that causes a fail?


Nothing special, just freshly installed FreeBSD 9.0 i386


----------



## fluca1978 (Apr 19, 2012)

I don't have a 9 machine right now to use for a test, I did it on my 8.2 one:

```
# uname -a
FreeBSD bsdmag 8.2-RELEASE FreeBSD 8.2-RELEASE 
#0: Fri Feb 18 02:24:46 UTC 2011     
root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
```

However, being it a shell script, I cannot see how the kernel could be the cause here!
I've tried both to run the script using an explicit 

```
/bin/csh test.csh
```
as well as 

```
./test.csh
```
and the result is always the same. Are you really sure there is nothing extra in the script? Could you please try it again copy and pasting from this forum the source code?


----------



## SirDice (Apr 19, 2012)

It must be some local issue. I tried it on a 9.0-RELEASE:

```
dice@vps-2417-1:~>./test.csh
0 - 7
0+1 - 25
0+1+1 - 160
dice@vps-2417-1:~>uname -a
FreeBSD vps-2417-1.tilaa.nl 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan  3 07:46:30 UTC 2012     
root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64
dice@vps-2417-1:~>csh --version
tcsh 6.17.00 (Astron) 2009-07-10 (unknown-unknown-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```

9.0-STABLE:

```
dice@williscorto:~>./test.csh
0 - 7
0+1 - 25
0+1+1 - 160
dice@williscorto:~>uname -a
FreeBSD williscorto.dicelan.home 9.0-STABLE FreeBSD 9.0-STABLE #0: Thu Apr 12 13:45:08 CEST 2012     
root@molly.dicelan.home:/usr/obj/usr/src/sys/CORTO  amd64
dice@williscorto:~>csh --version
tcsh 6.18.01 (Astron) 2012-02-14 (x86_64-amd-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```

And an old 8.2-STABLE:

```
dice@maelcum:~>./test.csh
0 - 7
0+1 - 25
0+1+1 - 160
dice@maelcum:~>uname -a
FreeBSD maelcum.dicelan.home 8.2-STABLE FreeBSD 8.2-STABLE #0: Sun Oct 30 19:08:33 CET 2011     
root@maelcum.dicelan.home:/usr/obj/usr/src/sys/MAELCUM  i386
dice@maelcum:~>csh --version
tcsh 6.17.00 (Astron) 2009-07-10 (i386-intel-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```


----------



## Ajax (Apr 19, 2012)

Yep. This is sick but I have two machines running the same installation of 9.0 and script works on one of them and doesn't on another. Diggin'â€¦


----------



## SirDice (Apr 19, 2012)

The only thing I could think of is some set option. Can you compare those?

This is on my 9.0-RELEASE machine:

```
dice@vps-2417-1:~>set
_

addsuffix
argv    ()
csubstnonl
cwd     /home/dice
dirstack        /home/dice
echo_style      bsd
edit
filec
gid     1001
group   dice
history 100
home    /home/dice
killring        30
loginsh
mail    /var/mail/dice
owd
path    (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin /home/dice/bin)
prompt  %{\033]0;%n@%m:%~\007%}%n@%m:%~%#
prompt2 %R?
prompt3 CORRECT>%R (y|n|e|a)?
savehist        (100 merge)
shell   /bin/tcsh
shlvl   1
status  0
tcsh    6.17.00
term    screen
tty     pts/3
uid     1001
user    dice
version tcsh 6.17.00 (Astron) 2009-07-10 (unknown-unknown-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
```


----------



## Ajax (Apr 19, 2012)

Would you believe?â€¦ However this user has /usr/local/bin/bash as shell set; used [CMD="pw user mod "]ajax -s /bin/csh[/CMD] to change shell but that didn't help.

```
%./test.csh 
0 - 7
%set
_	./test.csh 

addsuffix	
argv	()
csubstnonl	
cwd	/home/ajax
dirstack	/home/ajax
echo_style	bsd
edit	
filec	
gid	0
group	wheel
history	100
home	/home/ajax
killring	30
mail	/var/mail/ajax
owd	
path	(/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin /home/ajax/bin)
prompt	%
prompt2	%R? 
prompt3	CORRECT>%R (y|n|e|a)? 
savehist	100
shell	/bin/csh
shlvl	2
status	0
tcsh	6.17.00
term	xterm-color
tty	pts/0
uid	1001
user	ajax
version	tcsh 6.17.00 (Astron) 2009-07-10 (i386-intel-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
%
```


----------



## SirDice (Apr 19, 2012)

Well, I'm stumped 

The last thing I could think of is some locale(1) setting that's causing trouble.


----------



## fluca1978 (Apr 19, 2012)

Could it be a compromised shell? Are the checksums the same on the machines? Are the libraries the same against the shell?

```
~> ldd /bin/csh
/bin/csh:
        libncurses.so.8 => /lib/libncurses.so.8 (0x280d8000)
        libcrypt.so.5 => /lib/libcrypt.so.5 (0x28118000)
        libc.so.7 => /lib/libc.so.7 (0x28131000)
```

I'm just guessing, it is really strange.


----------



## freethread (Apr 19, 2012)

Ajax said:
			
		

> ```
> #!/bin/csh
> 
> set servers = (7 25 160)
> ...



I don't exactly know the result of the red line, it probably should be as follow?


```
my bad, this is wrong
    [red][del]@ i = i + 1[/del][/red]
```
or

```
@ i = $i + 1
```
or

```
@ i++
```

Did not know it was possible to use the plus sign in a set assignment not wrapped in single/double quotes to have a string concatenation as in:

```
set i = abcd${j}efgh
```

However it should work the same way on all systems (perhaps).


----------



## peckmex (May 26, 2012)

*Newline at end?*

Make sure you have a newline after the 'end' statement.


----------

