>我有两个变量如下
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=$?