用这个bash文件名提取技术?

我有一个bash脚本的一部分,得到一个没有扩展名的文件名,但我试图了解这里真正发生了什么。 什么是“%%”? 有人可以详细说明bash在幕后做了什么吗? 这种技术如何在一般基础上使用?

#!/bin/bash for src in *.tif do txt=${src%%.*} tesseract ${src} ${txt} done 

Solutions Collecting From Web of "用这个bash文件名提取技术?"

它取消了文件扩展名( 这里是.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 

来自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. 

这里是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. 

显然bash有几个“ 参数扩展 ”工具,其中包括:

只需替换值…

 ${parameter} 

展开为子字符串…

 ${parameter:offset} ${parameter:offset:length} 

替换参数值的长度…

 ${#parameter} 

在参数开始处扩展匹配…

 ${parameter#word} ${parameter##word} 

在参数结束时扩展匹配…

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

扩展参数来查找和替换字符串…

 ${parameter/pattern/string} 

这些是我认为我从手册页的这一部分理解的部分的解释。 让我知道如果我错过了一些重要的东西。

在bash手册页查看“Parameter Expansion”。 这个语法扩展了$ src变量,删除了与。*模式匹配的东西。

这是一个字符串删除操作格式: ${str%%substr}

其中str是您正在操作的字符串,substr是要匹配的模式。 它寻找str中substr的最长匹配,并从这一点上删除所有的东西。