用于这种bash文件名提取技术?

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

#!/bin/bash

for src in *.tif
    do
    txt=${src%%.*}
    tesseract ${src} ${txt}
    done

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

这些是我对我认为从手册页的这一部分理解的部分的解释。 如果我错过了重要的事情,请告诉我。

链接地址: http://www.djcxy.com/p/9791.html

上一篇: Uses for this bash filename extraction technique?

下一篇: Check if a program exists in bash