Bash shell for loop process with two paired variable names
It's common to use bash shell for example to bulk rename plenty files. Usually I used the following structure,
for file in ./*.short
do
# do command
done
That's for filename with .short extension. But, now how to process with two or more variable names (in this case file extension) in command? I would like to do bulk process for the following command,
( x2x +sf < data.short | frame -l 400 -p 80 | \
bcut +f -l 400 -s 65 -e 65 |\
window -l 400 -L 512 | spec -l 512 |\
glogsp -l 512 -x 8 -p 2 ;\
\
bcut +f -n 20 -s 65 -e 65 < data.mcep |\
mgc2sp -m 20 -a 0.42 -g 0 -l 512 | glogsp -l 512 -x 8 ) | xgr
In that case I have .short and .mcep that I want to process concurrently. I use the and logic (&&) but it didn't works,
for file1 in ./*.short && ./*.mcep
do
# $file1 and file2 process
done
Any experienced and skilled shell programmer want to share how to solve this? I have idea to used nested loops, but I don't know how to implemented in Bash.
Solution 1:
You can loop through the *.shorts and then check for corresponding *.mcep files with:
#!/bin/bash
for i in *.short
do
base=${i%%?????}
if [ -e ${base}mcep ]
then
echo ${base}.short
echo ${base}.mcep
fi
done
I just echoed the *.short and *.mcep names here but you can now use them in commands.
Solution 2:
You can use this script
#!/bin/bash
for file1 in /path/to/files/*
do
ext=${file#*.}
if [[ "$ext" == "mcep" ]]
then
#command to run on files with 'mcep' extension
elif [[ "$ext" == "short" ]]
then
#command to run on files with 'short' extension
fi
done