foreach loop working in terminal but not in the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach loop working in terminal but not in the script
# 1  
Old 07-27-2011
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:


Code:
#!/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:
Code:
1
2
9
40

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

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

Last edited by pludi; 07-27-2011 at 06:03 PM..
# 2  
Old 07-27-2011
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?
# 3  
Old 07-27-2011
Well maybe you isolated the problem. I went into my csh by typing csh in my terminal, then I type:
Code:
csh --version

And got:
Code:
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.
# 4  
Old 07-27-2011
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?
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-27-2011
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!
# 6  
Old 07-27-2011
Do echo $SHELL in your interactive terminal (not the script).
# 7  
Old 07-27-2011
Is that my problem it links to bash:

Code:
echo $SHELL

Yields:
Code:
/bin/bash

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

if this is the case do it do by:

Code:
setenv SHELL = /bin/csh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach loop with two variables

I need to put together a script that will take the contents of two different files (database name and database owner) and put them in two variables within a line: foreach x (`cat /local/hd3/dba/tools/build_db_scripts/dbs`) foreach z (`cat /local/hd3/dba/tools/build_db_scripts/dbas`)... (6 Replies)
Discussion started by: deneuve01
6 Replies

2. Shell Programming and Scripting

Foreach .gz file in a directory -- Not working

Hi, I am trying to add for loop in my code for all the .gz files in a directory. example code: for i in $PWD/input/*.dz do echo " file is :: $i " done This is not working as expectation and the output is : file is :: /home/IB/input/*.dz However, for i in $PWD/input/*... (5 Replies)
Discussion started by: IB_88
5 Replies

3. Shell Programming and Scripting

PERL foreach & open not working together

My script is as below: my $tile_list = `egrep "FCFP_TILE_LIST.*=" ${BudgetDir}/tile.params | sed -e 's/FCFP_TILE_LIST//' | sed -e 's/=//'`; print "Tile List = ".$tile_list."\n"; my @tiles = split(/\s+/, $tile_list); $unconst_out = "${DestDir}/Unconstrained_ports.rpt"; $check_tim_out =... (2 Replies)
Discussion started by: kuchi7
2 Replies

4. Shell Programming and Scripting

foreach loop in unix script

Hi all, I have a script which searches for all sql files in the current directory and replaces all sql files with an underscore with a dash. The next part I need to do is record the number of changes made (underscore to dash) and display this value (e.g.2). This is what I have so far; find /... (17 Replies)
Discussion started by: shawi
17 Replies

5. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

6. Shell Programming and Scripting

Using sed with a foreach loop

So I am back again beating my head against the wall with a shell script and getting a headache! I want to change each year in a file (1980, 1981, 1982, 1983, etc.) to the same year followed by a tab. The input is "blah blah (1980) blah blah". I want to get "blah blah (1980 ) blah blah".... (2 Replies)
Discussion started by: Peggy White
2 Replies

7. 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

8. Shell Programming and Scripting

foreach loop + 2 variables

In a foreach loop, is it possible for the loop to go through 2 arguments instead of one i.e. instead of foreach i (do stuff for i), we have foreach i j(do stuff for i; do stuff for j) I am working under BASH and TCSH shell environments cheers (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. 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

10. 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
Login or Register to Ask a Question