foreach loop working in terminal but not in the script

Hi, I am new here I have used the forums a long time to search for things they are very helpful. I have unfortunately used up all my resources (professors, grad students) and need some help.

I have this very simple piece of code (using to isolate the problem) in a csh script:

#!/bin/csh
foreach x(1 2 9 40)

	echo $x
	
end

When I actually copy and paste this into my terminal it works fine and outputs:

1
2
9
40

But when this exact piece of code is in the .csh file and I csh fileName.csh it outputs:

1

It's not looping! Any Ideas would be great thanks. I am beating my head over this one.

It works fine in a script for me. What version of csh do you have? Is it possible that /bin/csh isn't the same csh you get when you type csh on the commandline?

Well maybe you isolated the problem. I went into my csh by typing csh in my terminal, then I type:

csh --version

And got:

tcsh 6.15.00 (Astron) 2007-03-03 (x86_64-apple-darwin) options wide,nls,dl,al,kan,sm,rh,color,filec

does that mean I am using a tsch shell not a csh? If so how can I get/install a csh. I am on an internship and my professor uses csh, so I would like to be able to code on my computer. That very well could be my problem, I am not sure on the syntax for a tsch shell.

tcsh is just an improved csh(higher performance -- filename hashing and such) and ought to be compatible. Probably what your prof has too. Try 'whereis csh' to make sure there's not more than one on your system, though.

You're only 0.01 version away from the csh I have and it works with your script. I don't know why your script isn't working. Did you write it on a different machine and transfer it, or edit it right there?

1 Like

I did the 'whereis csh' and there is only one locate in '/bin/csh' just as it should be. I wrote it on my machine and tried compiling and it wont work. As soon as I move it to the linux cluster it works fine. I don't know what the problem is... it is clearly my machine, I will do some more searching on Apple forums. Thanks a lot!

Do echo $SHELL in your interactive terminal (not the script).

Is that my problem it links to bash:

echo $SHELL

Yields:

/bin/bash

I assume it needs to say '/bin/csh'?

if this is the case do it do by:

setenv SHELL = /bin/csh

The shell that you're using is bash, so you need to either A.) change your login shell (located in /etc/passwd), B.) run the csh shell and use that interactively (aka, just run /bin/csh or wherever that file is once you've logged in and you'll be using that shell. Type 'exit' to return to your regular bash shell)) or C.) if you're logging in through the ssh program, you can use the -t shell_name_goes_here option to try to login with that shell (as long as it's allowed in /etc/shells, if I recall correctly).

1 Like

Well there's your problem. You don't compile shell scripts.

I guess compile is in the right word, it should be run.

chmod +x code.csh

then:

./code.csh

Today after work I will be trying some other loops. Thanks for all the help.

No. Your current shell has nothing to do with the shell it uses when you 'csh programname' or './programname'

I don't know if you guys care, but since you helped I finally got back to town and for some reason it didn't like the white space in my loop from the editor!

Did you edit it in Windows? That'd do it.