foreach shell question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers foreach shell question
# 1  
Old 07-07-2009
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

Code:
 
#!/bin/sh

foreach i (1-365)
./data_make program <<EOF
'echo $i' 
 
/data_'echo $i'  #output file

I just dont know how to input a range without manually typing it in.
I've searched but had no luck, any ideas are appreciated.
Thanks
# 2  
Old 07-07-2009
Code:
#!/bin/sh
for ((i=1; i<=365; i++)); do
  echo $i | ./data_make program >> data_$i
done

(input from stdin)

Or

Code:
#!/bin/sh
for ((i=1; i<=365; i++)); do
  ./data_make program $i >> data_$i
done

(input as an argument)
# 3  
Old 07-07-2009
Actually, since Bash 3.0, you can do

for i in {1..365} ; do ./data_make program $i ; done

See Bash 3.00 brace expansion
# 4  
Old 07-08-2009
Ok my first attempt at suggestion with more info. I get an error: badly placed ()s
any ideas?

Code:
for ((i=1; i<=365; i++)); do
./window_sect_data_make <<EOF
echo $i  #!the record number
Vn  #! input
200 #! input
150 #! input
Color
velocity_data/filtered_data_$i; #outputfile name
done

Does it matter that there are inputs that dont change, and only one that does? Probably not but Im running out of ideas as to why this wont run.
Thanks

Last edited by d_kowalske; 07-08-2009 at 10:32 AM.. Reason: changed code
# 5  
Old 07-08-2009
On which line to you get the error? (please show the actual error message in full)

Is this the complete script?

Where is the closing EOF?
# 6  
Old 07-08-2009
I am not sure how to tell what line the error is on? I use
source shellscriptname to run
Code:
Badly placed ()'s.

I forgot last EOF but nothing changed
# 7  
Old 07-08-2009
Hmm. You're right. That for doesn't work in sh!

Change line 1 to:
Code:
#!/usr/bin/bash

The system I wrote it on yesterday had a link from sh to bash.

The system I'm using now doesn't - that's why it worked for me yesterday! Sorry about that.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach alternative - C shell

Hi all I wrote a foreach loop in c-shell: 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? ... (4 Replies)
Discussion started by: Somename
4 Replies

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

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

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

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

6. UNIX for Dummies Questions & Answers

foreach question

OK, so I am extremely rusty and am just getting back to Unix after 9 years. I'm stuck on something easy. I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. This works fine to do the search: while read i; do grep $i file2; done... (3 Replies)
Discussion started by: moldoverb
3 Replies

7. Shell Programming and Scripting

simple tcsh question using foreach

I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. I'm doing this in tcsh This works fine to do the search: while read i; do grep $i file2; done <file1.txt This also works fine to read a directory: foreach file ('/bin/ls... (1 Reply)
Discussion started by: moldoverb
1 Replies

8. UNIX for Dummies Questions & Answers

Linux Shell Question: how to print the shell script name ?

Suppose I have a script named "sc.sh" in the script how to print out its name "sc.sh"? (3 Replies)
Discussion started by: meili100
3 Replies

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

10. UNIX for Dummies Questions & Answers

foreach loop question

Hello, I am new at this forum so please bare with me on this. Within a given directory, I have a list of files in which in each file, I would like to do a substitution. I would like to substitute the string mlcl to mll in each file using the foreach command. I dont quite get how to do that. If... (7 Replies)
Discussion started by: clipski
7 Replies
Login or Register to Ask a Question