Bash:显示空白值的回显



>我有两个变量如下

SampleOutput=`some command giving output`
Status=`echo "$SampleOutput" | grep -qs "Active"`
echo $SampleOutput
echo $Status

在这里,$SampleOutput具有AgentEnable=Active bla bla bla的价值

但是,$Status来得blank我不确定为什么$Status在应该有价值时却是空白AgentEnable=Active

使用grep -q时,您不会从grep获得任何输出。只有退货状态可用,您可以使用:

grep -qs "Enable" <<< "$SampleOutput"
Status=$?

根据man grep

-q, --quiet, --silent安静模式:抑制正常输出。 grep 只会搜索文件,直到找到匹配项,这使得搜索可能更便宜。

请注意,如果您没有在其他任何地方使用SampleOutput,则可以直接使用:

some command | grep -qs "Enable"
Status=$?

最新更新