Dealing with repetition
Objectives
Repeat actions on multiple files using for loops and pathname expansion (AKA globbing)
Iterate on values using
seq
To follow along
Please navigate to the examples/dealing_with_repetition
directory
Iterating on multiple files/directories with for
loops
Example:
for filename in data/*.dat
do
./process.sh "$filename"
done
What is happening here?
data/*.dat
is expanded to a list of file names via globbing or pathname expansion.
See it withecho
:echo data/*.dat
The variable
filename
is assigned one value from the list, for each time the loop body is executed
xargs
instead of for
If the loop body is a one-liner, an alternative is particulary convenient.
Typically we use first the command to generate the list
we want to iterate on.
For example:
find data -name '*.dat'
We can use a pipe and the xargs
command to
find data -name '*.dat' | xargs -I{} ./process.sh {}
Non integer parameter scans with seq
The for
loop has also another possible syntax,
for ((i=0;i<5;i++))
do
echo $i
done
Bash can do some arithmetic, but only with integers:
echo $((5+4))
seq
can be used to generate a sequence of floating point values:
for x in `seq 0.3 0.1 1.1`
do
./simulate "$x"
done