加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:28:01 所属栏目:Linux 来源:网络整理
导读:我已经找到了一些在类似情况下执行此操作的示例,但这是我编写的唯一一个 shell脚本,除了逐字运行命令之外还执行任何操作,所以我很难将这些示例应用于我自己的情况并需要一些手 – 持有 3 我只是想从MP4批量翻录音频.这个脚本有效: for f in *.mp4; ffmpeg -
我已经找到了一些在类似情况下执行此操作的示例,但这是我编写的唯一一个 shell脚本,除了逐字运行命令之外还执行任何操作,所以我很难将这些示例应用于我自己的情况并需要一些手 – 持有< 3 我只是想从MP4批量翻录音频.这个脚本有效:

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

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

解决方法

如果你正在使用bash

${f%%.mp4}

将给出没有.mp4扩展名的文件名.

尝试使用它像这样:

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

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

说明

bash手册(man bash)声明:

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

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变量执行的许多字符串操作之一.它们都以参数扩展名称命名.

这也是bash手册中给出的章节标签.因此man bash / paramter exp应该快速带你到那里.`

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读