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

前端之家收集整理的这篇文章主要介绍了bash for循环中的文件名不带扩展名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在这样的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.

猜你在找的Bash相关文章