bash脚本一次处理多个文件

时间:2017-07-07 13:24:15

标签: bash shell loops

我在.txt,.update,.text等文件夹中有多个扩展名的文件。文件名就像

out1.txt, out1.text, out1.update,

out2.txt, out2.text, out2.update

依此类推......我的命令如下:

./script.pl out1.txt out1.text out1.update

我想将此命令放在每个文件的循环中。我试过使用像:

这样的循环
for i in *.{txt,text,update}

但它需要包括扩展名在内的全名,所以我无法弄清楚如何在命令中推断出特定的扩展文件。

谢谢!

2 个答案:

答案 0 :(得分:0)

ext=$(awk -F\. '{ print "."$NF }' <<< $i)
fil=$(basename -s $ext $i)

使用awk提取扩展名(使用“。”分隔第二个数据) 使用basename -s和扩展名来获取没有扩展名的文件名

答案 1 :(得分:0)

你可以使用:

for f in *.txt;
do
    b=${f%.txt} # the trick is there
    ./script.pl $b.txt $b.text $b.update
done
相关问题