Foreach alternative - C shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Foreach alternative - C shell
# 1  
Old 01-18-2013
Foreach alternative - C shell

Hi all

I wrote a foreach loop in c-shell:

Code:
foreach file (/.../fileNames*)
...
end

The problem is that if there aren't matching files in the directory I'm getting a
"foreach: No match". How can I rewrite it so the script will just skip the loop if there aren't any matching files?

Thanks =)
# 2  
Old 01-18-2013
You can do:
Code:
foreach file (/.../fileNames*) >& /dev/null

Better would be to use a decent shell. C Shell is rather hopeless.
# 3  
Old 01-18-2013
not what I was looking for

Thank Scott!
I want the scrip to continue running after the loop, is there a way to do it?
I tried:
Code:
if (-e "/../fileName*") then foreach..

but c shell didn't recognize the *.

Is there another solution?

Thanks
# 4  
Old 01-18-2013
You cannot cram 37 arguments or 0 arguments into an -e. It has to be one and only one.

You could try Scott's solution.

Better would be to use a decent shell. C Shell is rather hopeless.
# 5  
Old 01-18-2013
OK, thanks for the help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Homework & Coursework Questions

Alternative solution to nested loops in shell programming

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi, The problem statement is: I am trying to read line by line from a flat file by using a while loop. The... (9 Replies)
Discussion started by: Sandeep Pattnai
9 Replies

3. UNIX for Dummies Questions & Answers

Shell script foreach help

I am writing a shell script to uncompress files in a directory, then call a Perl script to search the files for given terms, store those terms in a different output file , and compress the output. I get a syntax error with my use of foreach. Below is my script. #!/bin/csh -fxv if (!... (2 Replies)
Discussion started by: dgrayman
2 Replies

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

5. Shell Programming and Scripting

Shell script: foreach file -> targzip

Hi there, I need some help with a shell script (I'm no sh script expert, but I hope this will explain how I want my script):dir = /home/user/files/ foreach(*.jpg file in $dir) { tar -cf $file(-.jpg).tar $file;gzip $file(-.jpg).tar } mv -f $dir*tar.gz /home/user/pictures/ Thanks for any... (12 Replies)
Discussion started by: JKMlol
12 Replies

6. UNIX for Dummies Questions & Answers

foreach shell question

Hi I would like foreach to go through a range of numbers 1-365. This input is being read by a compiled fortran program in the same shell script. Let me try an example to clarify #!/bin/sh foreach i (1-365) ./data_make program <<EOF 'echo $i' /data_'echo $i' #output file I... (10 Replies)
Discussion started by: d_kowalske
10 Replies

7. Shell Programming and Scripting

Shell Integer with nested foreach

I am scripting in tcsh and here is what I currently have: foreach group (g1 g2 g3 g4) set ppl = `cat $group.file.with.list.of.ppl.in.row.format` set label = 1 @ label += 1 foreach ppls ($ppl) echo $label >> file end end ... (0 Replies)
Discussion started by: bonesy
0 Replies

8. Shell Programming and Scripting

C Shell - foreach - No Match error

Hi All, I am facing 'No Match' problem with foreach loop in C shell script. Initially I tried following grep command showing results properly as shown at the end of the Thread. But foreach command is throwing the error 'No match'. grep -n Inserted audit_file foreach insertstr (`grep -n... (0 Replies)
Discussion started by: adurga
0 Replies

9. Shell Programming and Scripting

Shell alternative for setlocale()

Hi all Is there any way I could set "locale" variables other than writing a C-program using the "setlocale" function? IŽd like to be able to set it from a shell script. Cheers (3 Replies)
Discussion started by: Indalecio
3 Replies

10. UNIX for Dummies Questions & Answers

foreach in shell scripting

I need to read list of machines from a file using foreach loop. I am trying the follwing, but its not reading the list foreach i (`cat file.lst | awk '{print $1}'`) ls -l | grep $i end here the file file.lst contains list of files Any idea whats wrong here Thanks Krisyet (2 Replies)
Discussion started by: krisyet
2 Replies
Login or Register to Ask a Question