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

bash for循环中的文件名不带扩展名

发布时间:2020-12-15 16:17:14 所属栏目:安全 来源:网络整理
导读:在这样的for循环中: for f in `ls *.avi`; do echo $f; ffmpeg -i $f $f.mp3; done $ f将是完整的文件名,包括扩展名。例如,对于song1.avi,命令的输出将是song1.avi.mp3。有没有办法只有song1,没有。环境中的.avi? 我想象有办法使用awk或其他这样的工具
在这样的for循环中:
for f in `ls *.avi`; do echo $f; ffmpeg -i $f $f.mp3; done

$ f将是完整的文件名,包括扩展名。例如,对于song1.avi,命令的输出将是song1.avi.mp3。有没有办法只有song1,没有。环境中的.avi?

我想象有办法使用awk或其他这样的工具,但我希望有一些更直接的东西。

谢谢

使用bash参数展开
${f%%.*}

请注意,您需要贪婪版本,因为文件名中有多个点。

从bash手册:

${parameter%word}

${parameter%%word}

The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter,then the result of the expansion is the 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.

(编辑:李大同)

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

    推荐文章
      热点阅读