使用此bash的文件名萃取技术? [英] Uses for this bash filename extraction technique?

查看:70
本文介绍了使用此bash的文件名萃取技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是得到一个文件名没有扩展名bash脚本的一部分,但我想知道究竟怎么回事就在这里。什么是%%的呢?谁能细说什么bash的是做幕后?如何解决这个技术来一般的基础上使用?

 #!/斌/庆典在* .TIF SRC
    做
    TXT = $ {SRC %%。*}
    的tesseract $ {SRC} $ {} TXT
    DONE


解决方案

它摆脱了文件的扩展名(这里的.tif ) ,样本:

  $对于A在test.py test.sh的test.xml test.xsl;做回声$ A:$ {A %%。*}; DONE
test.py:测试
test.sh:测试
的test.xml:测试
test.xsl:测试

从bash的手册:

  $ {参数%%词}
          word被扩展为一个模式,就像路径扩展。如果
          模式参数扩展后的值的尾部相匹配,那么,
          扩张的结果是参数的最短扩展值
          匹配(``%''的情况)或者最长的匹配(``%%''
          情况)删除。如果参数是@或*,模式删除应用
          要依次在每个位置参数,最后扩展为结果的列表。
          如果参数是@或*,模式删除下标数组变量
          操作应用于反过来阵列的每个成员,并且膨胀是
          结果的列表。

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?

#!/bin/bash

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

解决方案

It gets rid of the filename extension (here: .tif), sample:

$ 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:

   ${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的文件名萃取技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆