且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Linux Shell脚本中更改文件扩展名?

更新时间:2022-01-25 09:00:39

如果您使用的是bash

If you're using bash

${f%%.mp4}

将提供不带.mp4扩展名的文件名.

will give the filename without the .mp4 extension.

尝试像这样使用它:

for f in *.mp4; do
    ffmpeg -i "$f" -f mp3 -ab 192000 -vn "mp3s/${f%%.mp4}.mp3"
done

...并且不要忘记给出的示例中的do关键字.

... and don't forget the do keyword as in the example given.

bash手册(man bash)指出:

The bash Manual(man bash) states:

$ {parameter%word} $ {parameter %% word}

${parameter%word} ${parameter%%word}

删除匹配的后缀模式. 这个词被扩展为产生 一个与路径名扩展中一样的模式.如果模式匹配 参数扩展值的尾部,然后 扩展的结果是参数的扩展值 最短的匹配模式(%'' case) or the longest matching pattern (the %%''情况)被删除.如果参数为@ 或*,将模式去除操作依次应用于每个位置参数,然后进行扩展 列表.如果parameter是一个用@或下标的数组变量 *,图案去除操作适用于 依次排列数组,展开就是结果列表.

Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the %'' case) or the longest matching pattern (the%%'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

这只是可以对shell变量执行的许多字符串操作之一.它们都以参数扩展的名称命名.

This is just one of many string manipulations you can perform on shell variables. They all go by the name of Parameter Expansion.

这也是bash手册中给出的部分标签.因此,man bash /paramter exp 应该可以带您快速到达那里. `

That's as well the section label given in the bash manual. Thus man bash /paramter exp should bring you there fast. `