【问题标题】:Why is my Bash script with grep not working?为什么我的带有 grep 的 Bash 脚本不起作用?
【发布时间】:2020-07-04 21:18:45
【问题描述】:

我试图理解为什么这个非常简单的脚本不起作用:

echo "SomeText" > test
if [[ "grep 'FindMe' ./test" ]] ; then
  echo Why is this line written to screen?
fi

我试过了:

"grep 'FindMe' ./test"

$(grep 'FindMe' ./test)

`grep 'FindMe' ./test`

还有很多 grep 选项。

我尝试过使用[][[]]{} 以及任何组合。

【问题讨论】:

    标签: bash if-statement grep


    【解决方案1】:

    其实要检查退出状态,不用括号也不用引号:

    if grep 'FindMe' ./test ; then
    

    使用grep -q 在我们不希望脚本的输出被随机匹配混乱的情况下很常见。

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2023-02-05
      • 2016-11-03
      相关资源
      最近更新 更多