I have written a small bash* script to process files in a directory. The files have a specific naming convention, which determines how the file is to be processed.
The filename starts with a geographic origin, and then contains a keyword in the filename that determines how it should be processed.
Here is the stripped down version of my script:
DATA_DIRECTORY=./data
array=( 'uk' 'usa' 'fra' 'ukr' )
for i in "${array[@]}"
do
echo "#################################################"
echo "Loop index : $i"
echo "#################################################"
# process baboon data for the current country
for filename in $DATA_DIRECTORY/$i_*_baboon*.csv; do
echo "Processing file: $filename in loop: $i"
done
# process whale data for the current country
for filename in $DATA_DIRECTORY/$i_*_whale*.csv; do
echo "Processing file: $filename in loop: $i"
done
# process lion data for the current country
for filename in $DATA_DIRECTORY/$i_*_lion*.csv; do
echo "Processing file: $filename in loop: $i"
done
done
I am expecting the script above to match the files in the following way: A file named us_2007_deaths_caused_by_baboon.csv will be processed once – when the loop counter variable is ‘us’ and during the loop for filename in $DATA_DIRECTORY/$i_*_baboon*.csv;. However, the matching is not working as I expected, and each (nested for loop) is looping over all of the files in the directory – which is NOT what I want.
How may I pattern match the files based on the filenames?
*Note: I am using bash version 4.1.5 on Ubuntu 10.0.4
Write
${i}_instead of$i_because$i_is “” (bash thinks thati_is a variable here).For example: