Mathi
Mathi

Reputation: 45

Bash read multiple files in for loop (as a execute arguement)

I am trying to read a file in bash forloop. But I do not know how to put write the script for this.

for i in $( seq 0 $step 10 )
do
    echo "Rendering: "$(( i + j ))
    python auto_fine.py density000000.vtu velocity000000.vtu $(( i + j ))
done

each and every loop I need to call

i -> 0 python auto_fine.py density000000.vtu velocity000000.vtu

i -> 1 python auto_fine.py density000010.vtu velocity000010.vtu

i -> 2 python auto_fine.py density000020.vtu velocity000020.vtu

Upvotes: 0

Views: 218

Answers (3)

ghoti
ghoti

Reputation: 46856

Bash can do this without requiring external tools like seq.

for i in {0..100}; do
  [[ $i = *0 ]] || continue
  python auto_fine.py density$(printf '%06d' $i).vtu velocity$(printf '%06d' $i).vtu
done

This uses pattern matching (*0) to limit your list to every 10 numbers, which is a bit of a hack, but will work against your sample data.

You could alternately loop against your zero-padded numeric strings directly:

for i in $(printf '%05d0 ' {0..10}); do
  python auto_fine.py density$i.vtu velocity$i.vtu
done

This option shows you every 10 items by placing a zero in the printf format after the incrementing number, which becomes the tens digit. If you want more arbitrary sequencing, you might use multipliers, still without spawning external processes:

low=0
high=100
mult=10

for i in $(eval echo {$low..$((high/mult))}); do
  n=$(printf '%06d' $((i*mult)))
  python auto_fine.py density$n.vtu velocity$n.vtu
done

Note the eval, which lets you expand variables for use in your sequence expression. (If you are getting these numbers from an external source, have your script validate them before using them!)

If you're using bash version 4 (i.e. not the native version on OSX), you also have increments available in sequence expressions. From the man page:

A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer.

So perhaps:

low=0
high=100
mult=10

for i in $(eval "printf '%06d ' {$low..$high..$mult}"); do
  python auto_fine.py density$i.vtu velocity$i.vtu
done

Note that in sequence expressions, the first member of the sequence is the first number provided, rather than merely a product of a multiplier. We have quotes around the printf to ensure that the sequence expression is expanded by eval, and not interpreted by the command substitution ($(..)).

Upvotes: 1

fedorqui
fedorqui

Reputation: 289755

It seems to me that you need to zero pad the numbers sed provides to you:

As seen in How to zero pad a sequence of integers in bash so that all have the same width?, you need to do something like

$ seq -f "%06g" 0 10 100

Which returns:

000000
000010
000020
...
000100

All together,

for i in $(seq -f "%06g" 0 10 100)
do
    # echo "Rendering: "$(( i + j )) -- not sure what this does
    python auto_fine.py density$i.vtu velocity$i.vtu
done

Upvotes: 2

godzillante
godzillante

Reputation: 1214

looping for all the files in the current dir is trivial:

for i in $( ls -1 )
do
    # your code here, variable is referenced with $i
done

what's the j variable you are using?

Upvotes: -1

Related Questions