代码之家  ›  专栏  ›  技术社区  ›  srdg

shell脚本中的${img_file%.*}是什么意思?

  •  0
  • srdg  · 技术社区  · 8 年前

    我知道 .* 意味着不管扩展名是什么,都可以获取所有文件(我希望我没有错)。然而,我无法为我的生命之爱似乎弄明白什么是额外的%符号的意思!

    以下两个代码片段可能有助于进一步描述这种情况:

    img_files=${img_files}' '$(ls ${TRAINING_DIR}/*.exp${exposure}.tif)
    
    for img_file in ${img_files}; do
            run_command tesseract ${img_file} ${img_file%.*} \
                ${box_config} ${config} &
    

    对于那些需要更多细节的人,这里是 full script

    2 回复  |  直到 8 年前
        1
  •  3
  •   builder-7000    8 年前

    表达 ${img_file%.*} 将删除变量中最右边的点及其后的任何字符 img_file . 从 man bash :

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

    例子:

    >var="word1 word2"
    >echo ${var%word2}
    word1
    >echo ${var%word1}
    word1 word2
    
        2
  •  0
  •   Ashishkumar Singh    8 年前

    %这里的意思是从右边移走。例如

    考虑一个变量img_file=“racecar”

    ${img_file%c*} 将返回 race

    ${img_file%%c*} = ra