【问题标题】:What does “${x%% *}” mean in sh?sh中的“${x%% *}”是什么意思?
【发布时间】:2013-05-07 08:14:38
【问题描述】:

我刚刚在 makefile 中看到“$${x%% *}”,这意味着 sh 中的“${x%% *}”。 为什么这样写?

https://unix.stackexchange.com/questions/28994/how-can-a-makefile-detect-whether-a-command-is-available-in-the-local-machine

determine_sum = \
        sum=; \
        for x in sha1sum sha1 shasum 'openssl dgst -sha1'; do \
          if type "$${x%% *}" >/dev/null 2>/dev/null; then sum=$$x; break; fi; \
        done; \
        if [ -z "$$sum" ]; then echo 1>&2 "Unable to find a SHA1 utility"; exit 2; fi

checksums.dat: FORCE
    $(determine_sum); \
    $$sum *.org

另外,如何在谷歌中搜索${x%% *}

【问题讨论】:

  • 谷歌“bash 手册页”
  • Google “如何使用 google bash 表达式” :) “所有 CS 问题都可以通过另一个级别的间接来解决”

标签: shell sh


【解决方案1】:

${x%% *} 用最左边的空格替换 x 变量的值,并删除它右边的所有内容。

http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

第二个问题我没有答案。

【讨论】:

    【解决方案2】:

    ${x%% *} 是删除除$x 变量中的第一个单词以外的所有内容的好方法。也就是说,给定文本XXX YYY ZZZ,此语法将删除第一个空格之后的所有内容。

    一些例子:

    $ x="aaa abbb"
    $ echo ${x%% *}
    aaa
    
    $ x="aaa abbb bccc defasdfs"
    $ echo ${x%% *}
    aaa
    

    相当于:

    $ x="aaa abbb bccc defasdfs"
    $ echo $x | awk '{print $1}'
    aaa
    

    为了清楚起见,我们再举一个例子:

    $ x="aaa abbb_bccc_defasdfs"
    $ echo ${x%%_*}
    aaa abbb
    

    我们查找第一个 _ 字符,然后从该点删除直到结束。

    要查看更多这些有趣的命令,您可以查看Reference Cards #String Operations


    关于 Google 的问题,是的,这些字符无法通过他们的搜索引擎进行搜索。相反,我建议您使用SymbolHound

    例如,querying ${x%% *} 显示有价值的结果。

    【讨论】:

    猜你喜欢
    • 2014-01-31
    • 2022-01-25
    • 2015-03-24
    • 1970-01-01
    • 2020-03-18
    • 2021-08-02
    • 2015-12-30
    • 2020-10-02
    • 2011-05-27
    相关资源
    最近更新 更多