用于此 bash 文件名提取技术?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/538504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uses for this bash filename extraction technique?
提问by jjclarkson
I have a portion of a bash script that is getting a filename without extension, but I'm trying to understand what is really going on here. What are the "%%"'s for? Can someone elaborate on what bash is doing behind the scenes? How can this technique be used on a general basis?
我有一部分 bash 脚本正在获取没有扩展名的文件名,但我试图了解这里真正发生了什么。“%%”是干什么用的?有人可以详细说明 bash 在幕后做了什么吗?如何在一般基础上使用这种技术?
#!/bin/bash
for src in *.tif
do
txt=${src%%.*}
tesseract ${src} ${txt}
done
回答by Johannes Weiss
It gets rid of the filename extension (here: .tif), sample:
它去掉了文件扩展名(这里是: .tif),示例:
$ for A in test.py test.sh test.xml test.xsl; do echo "$A: ${A%%.*}"; done
test.py: test
test.sh: test
test.xml: test
test.xsl: test
from bash manual:
来自bash手册:
${parameter%%word}
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.
回答by Steve Lazaridis
Here's output from the bash man page
这是 bash 手册页的输出
${parameter%%word}
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 pat-
tern (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.
回答by jjclarkson
Apparently bash has several "Parameter Expansion" tools which include:
显然 bash 有几个“参数扩展”工具,其中包括:
Simply substituting the value...
简单地替换值...
${parameter}
Expanding to a sub-string...
扩展为子字符串...
${parameter:offset}
${parameter:offset:length}
Substitute the length of the parameters value...
代入参数值的长度...
${#parameter}
Expanding upon a match at the beginning of the parameter...
在参数开头的匹配项上扩展...
${parameter#word}
${parameter##word}
Expanding upon a match at the end of the parameter...
在参数末尾扩展匹配...
${parameter%word}
${parameter%%word}
Expands the parameter to find and replace a string...
扩展参数以查找和替换字符串...
${parameter/pattern/string}
These are my interpretation of the parts I think I understand from this section of the man pages. Let me know if I missed something important.
这些是我对手册页这一部分中我认为我理解的部分的解释。如果我错过了什么重要的事情,请告诉我。
回答by Jeff
It's a string removal operation in the format: ${str%%substr}
这是一个字符串删除操作,格式如下: ${str%%substr}
Where str is the string you are operating on, and substr is the pattern to match. It looks for the longest match of substr in str, and removes everything from that point on.
其中 str 是您正在操作的字符串,而 substr 是要匹配的模式。它在 str 中查找 substr 的最长匹配项,并从该点开始删除所有内容。
回答by PEZ
Check out "Parameter Expansion" in the bash man pages. That syntax expands the $src variable deleting stuff that matches the .* pattern from it.
查看 bash 手册页中的“参数扩展”。该语法扩展 $src 变量,从中删除与 .* 模式匹配的内容。

