gpt4 book ai didi

linux - Bash:回显显示空白值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:07 25 4
gpt4 key购买 nike

我有两个变量如下

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

此处 $SampleOutput 的值为 AgentEnable=Active bla bla bla

但是,$Statusblank 我不确定为什么 $Status 应该有值时却变成空白 AgentEnable=Active

最佳答案

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

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

根据man grep:

-q, --quiet, --silent Quiet mode: suppress normal output. grep will only search a file until a match has been found, making searches potentially less expensive.

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

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

关于linux - Bash:回显显示空白值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52493610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com