从完整文件名中提取文件名

发布于 2024-12-09 23:21:14 字数 183 浏览 0 评论 0原文

#!/bin/bash
FILES=src/*.erl
shopt -s nullglob
for f in $FILES
do
  echo "Processing $f file..."
# ???
done

如何从完整路径中提取文件名? “pathfilename=${f%.*}”是什么意思?

#!/bin/bash
FILES=src/*.erl
shopt -s nullglob
for f in $FILES
do
  echo "Processing $f file..."
# ???
done

How i can extract file name from full path? And what does it mean - "pathfilename=${f%.*}"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-12-16 23:21:14

我将复制帮助输出,因为它有示例和所有内容。

~$ basename --help
Usage: basename NAME [SUFFIX]
  or:  basename OPTION
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.

      --help     display this help and exit
      --version  output version information and exit

Examples:
  basename /usr/bin/sort       Output "sort".
  basename include/stdio.h .h  Output "stdio".

I'll just copy the help-output, since it has examples and everything.

~$ basename --help
Usage: basename NAME [SUFFIX]
  or:  basename OPTION
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.

      --help     display this help and exit
      --version  output version information and exit

Examples:
  basename /usr/bin/sort       Output "sort".
  basename include/stdio.h .h  Output "stdio".
微凉 2024-12-16 23:21:14

更新:我已经删除了对第一部分的回答,因为显然我误解了所问的内容。

您提到的语法,pathfilename=${f%.*} ,表示 pathfilename 设置为 $f 的值,并从字符串末尾删除 .* 的最短可能匹配项。这将从文件名中去除扩展名。 bash 手册对此语法的描述如下 :

${参数%word}

${参数%%字}

删除匹配的后缀模式。该单词被扩展以产生一个模式,就像路径名扩展一样。如果模式与参数扩展值的尾部部分匹配,则扩展的结果是具有最短匹配模式(“%”情况)或最长匹配模式(“%%”情况)的参数扩展值)删除。如果参数是@或*,则依次对每个位置参数应用模式删除操作,并且扩展是结果列表。如果parameter是下标为@或*的数组变量,则依次对数组的每个成员应用模式删除操作,并且扩展是结果列表。

Update: I've removed my answer to the first part, since apparently I misunderstood what was being asked.

The syntax you mention, pathfilename=${f%.*}, means that pathfilename is set to the value of $f with the shortest possible match for .* removed from the end of the string. This will strip the extension from the filename. The bash manual describes this syntax as follows:

${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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文