如何在Linux Shell脚本中更改文件扩展名? [英] How to change file extension in Linux shell script?

查看:130
本文介绍了如何在Linux Shell脚本中更改文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了在类似情况下执行此操作的一些示例,但这是我编写的唯一一个脚本,除了逐字逐句运行命令之外,该脚本不执行任何其他操作,因此我努力将这些示例应用于我自己的情况和需要一些手持的< 3

I've found some examples of doing this in similar situations, but this is the only shell-script I've written that does anything besides run commands verbatim, so I am struggling to apply the examples to my own situation and need some hand-holding <3

我只是想从MP4批处理翻录音频.该脚本有效:

I'm just trying to batch rip audio from MP4s. This script works:

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

但是文件都以.mp4.mp3结尾.如何摆脱mp4部分?

But the files all end with .mp4.mp3. How can I get rid of the mp4 part?

推荐答案

如果您使用的是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. `

这篇关于如何在Linux Shell脚本中更改文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆