【问题标题】:How do I use grep for returning the bool value of this variable?如何使用 grep 返回此变量的布尔值?
【发布时间】:2014-06-01 20:08:48
【问题描述】:

我希望 stout 在以下位置返回“备用”变量的布尔值:

pmset -g | grep standby

输出,显然这里是:

standbydelay         10800
 standby              0 #<--This is what I want!#

另外:

pmset -g | grep -cim1 standby 

返回1,表示仅存在字符串。它没有给我任何关于它的布尔值的信息。

将 bool 值“0”打印到 stout 的语法是什么?我怀疑使用 awkprint 需要额外的参数,但我不知道。先感谢您。

【问题讨论】:

    标签: bash unix awk grep boolean


    【解决方案1】:

    会很简单

    $ ... | awk '/ standby/{print $2}'
    0
    

    做这份工作吗?

    【讨论】:

    • +1。您也可以使用$1 == "standby" { print $2} 缩小范围
    【解决方案2】:

    我同意 Fredrik Pihl 的建议。但是,您也可以利用 grep 的退出代码:

    if grep -q 'standby  *1' file
    then
      echo "It's set to 1"
    else
      echo "It's either set to 0 or not present"
    fi
    

    【讨论】:

    • 交互式命令不限于一行,因此您可以完全按照换行符和所有内容的方式键入/粘贴。如果你想要它在一行中,if grep -q 'standby *1' file; then echo "foo"; else echo "bar"; fi
    【解决方案3】:

    GNU sed:

    $ com () { echo "standbydelay         10800
     standby              0"; }
    $ com | sed -n 's/ standby[[:space:]]\+//p'
    0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 2018-01-12
      • 2023-03-13
      • 1970-01-01
      • 2010-11-30
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多