Bash shell for循环过程有两个配对的变量名

例如,使用bash shell批量重命名大量文件是很常见的。 通常我使用以下结构,

for file in ./*.short do # do command done 

那是带有.short扩展名的文件名。 但是,现在如何在命令中处理两个或多个变量名(在本例中为文件扩展名)? 我想为以下命令进行批量处理,

 ( 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 

在那种情况下,我有.short和.mcep,我想同时处理。 我使用逻辑(&&),但它不起作用,

 for file1 in ./*.short && ./*.mcep do # $file1 and file2 process done 

任何经验丰富,技术娴熟的shell程序员都想分享如何解决这个问题? 我有想法使用嵌套循环,但我不知道如何在Bash中实现。

您可以遍历* .shorts,然后检查相应的* .mcep文件:

 #!/bin/bash for i in *.short do base=${i%%?????} if [ -e ${base}mcep ] then echo ${base}.short echo ${base}.mcep fi done 

我刚刚在这里回显了* .short和* .mcep名称,但您现在可以在命令中使用它们。

您可以使用此脚本

 #!/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 
 #!/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