【问题标题】:Testing command substitution output in fish在fish中测试命令替换输出
【发布时间】:2015-03-11 09:17:46
【问题描述】:

another question 关于测试特定字符串的命令替换。我想测试一个命令是否在单个语句中输出 anything,即相当于:

if [[ -n "$(foo)" ]]

在 bash 中。鱼不认识[[]],而且

if [ -n "(foo)" ]  # No good, "(foo)" is literal.
if [ -n (foo) ] # Passes only if (foo) == "\n" because of test semantics.

不会工作意味着我必须这样做

set check (foo)
if [ -n "$check ]

有没有可能我在这里错过了?

【问题讨论】:

    标签: fish


    【解决方案1】:

    这应该可行:

    if count (foo) > /dev/null
    

    【讨论】:

    • 出于好奇,您认为这会比if [ (count (foo)) != 0 ] 贵还是便宜?我假设> /dev/null 版本保存了一个子shell...
    • @goldilocks 大约相等-fish 还不够聪明,无法在从内置输出到 /dev/null 时避开分叉,尽管它应该是。如果是省略分叉,count命令会更有效率。
    【解决方案2】:

    据我所知,Fish 中无法使用字符串替换。

    您可以关注#159 issue 以获取有关当前解决方案的更多信息。

    【讨论】:

      【解决方案3】:

      在fish中,我们必须使用test而不是[[[

      if test -n (foo)
        echo not empty
      end
      

      或者,bash [[ -n $(foo) ]] && echo not empty 的等价物是

      test -n (foo); and echo not empty
      

      【讨论】:

      • [ ] test。试试你的例子,例如if test -n (echo -n "") -- 你得到“不是空的”。 test -n(又名[ -n ])只会对"\n"失败。一个真正的空字符串(没有输出,如echo -n "")将通过
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多