1

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.

6
  • So you want to run one command in the files with .short extension and another command for the files with .mcep extension?
    – Rumesh
    Jun 4, 2015 at 0:28
  • Yes! But is mixed.
    – sugab
    Jun 4, 2015 at 0:39
  • Ok I'll try to write a script and tell if you it works
    – Rumesh
    Jun 4, 2015 at 0:53
  • 2
    Is there a matching .mcep file for each .short file? if so, you just need to loop over one and create the other by substitution e.g. "${file%.short}.mcep" Jun 4, 2015 at 1:12
  • 2
    @bagustris I have posted my answer
    – Rumesh
    Jun 4, 2015 at 2:32

3 Answers 3

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.

1

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
0
#!/bin/bash

for file in ./*.short 
do
(x2x +sf < $file | 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 < ${file%.short}.mcep |\
mgc2sp -m 20 -a 0.42 -g 0 -l 512 | glogsp -l 512 -x 8 ) | psgr > ${file%.short}.eps
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.