foreach shell question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers foreach shell question
# 8  
Old 07-08-2009
Scott, thanks for your help
I still have the same problem, I am a new user is it possible not to have bash and only sh? Let me post my entire code to see if it helps.
Code:
./window_sect_inf_make
echo "Finished: inf_make:"


for ((i=1; i<=365; i++)); do
 ./window_sect_data_make <<EOF
`echo $i`  #!the record number-for velocity its day number-use 12 for T/S
Vn  #! Options: T,S,Vn,
200 #! along transect resolution
150 #! depth
Color
velocity_data/filtered_data/gom1995.nc #input read file
EOF

echo "Finished: data_make:"

./window_sect_grrider <<EOF
sect_inf/window_sect.dat #input
1 #input
500000 1700000 -700000 350000 #input
200 150 #input
sect_inf/Vec_data/filtered/window_sect`echo $i`.grd #outputfile
1 #input
9 #input
0 #input
EOF
;done

I never make it to the line with
Code:
echo "Finished: data_make:"

# 9  
Old 07-08-2009
This for doesn't work in sh, but you can use a while loop instead...

Code:
i=1
while true $i -le 365; do
... your code
i=$((i+1))
done

As for the logic of your script, I'm struggling to see exactly what you're trying to do. The parts between the <<EOF and EOF all seem a bit jumbled up and it's not so clear what you expect as input and output.

i.e. Does all of the input come from "velocity_data/filtered_data/gom1995.nc" or also Vn, 200, 150, Color, etc?

Code:
`echo $i`  #!the record number-for velocity its day number-use 12 for T/S
Vn  #! Options: T,S,Vn,
200 #! along transect resolution
150 #! depth
Color
velocity_data/filtered_data/gom1995.nc #input read file

This is also very confusing
Code:
sect_inf/window_sect.dat #input (input)
1 #input (input)
500000 1700000 -700000 350000 #input (input)
200 150 #input (input)
sect_inf/Vec_data/filtered/window_sect`echo $i`.grd #outputfile (output)
1 #input (input)
9 #input (input)
0 #input (input)

Tip: the closing EOF must be at the very start of the line (with no leading whitespace). Don't use comments inside the EOF as they'll be taken as part of the input.
# 10  
Old 07-08-2009
There are multiple input files and multiple user input variables. Im just trying to automate something that was already written. I will try your suggestions.
Thanks for your help

---------- Post updated at 07:02 AM ---------- Previous update was at 06:52 AM ----------

When I run each program and do manual input everything works so I am convinced it is something with my shell. This is the new error, and my edited code.
Code:
Finished: inf_make:
i=1: Command not found.
i: Undefined variable.

new code
Code:
i=1
while true $i -le 365; do
./window_sect_data_make<<EOF
`echo $i`
Vn
200
150
Color
velocity_data/filtered_data/gom1995.nc
EOF

echo "Finished: data_make:"

./window_sect_grrider<<EOF
sect_inf/window_sect.dat
1
500000 1700000 -700000 350000
200 150
sect_inf/Vec_data/filtered/window_sect`echo $i`.grd
1
9
0
EOF
i=$((i+1))
done

# 11  
Old 07-14-2009
try

try these

for i in `seq 0 365`; do
echo $i
done

or

seq 0 365|while read line; do
echo $line
done
 
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