grep状态并循环它,直到shell中的count条件

grep状态并循环它,直到shell中的count条件,shell,scripting,grep,Shell,Scripting,Grep,我想检查状态,如果状态不是“正在运行”,我想让我的脚本休眠5秒并增加计数器 可以使用mcstat检查电池的状态 14:24:25 # mcstat -n cell1 XXX Impact InfoStatus 9.5.00 (Build 241196604 - 15-Jan-2014) [l2] Copyright 1998-2014 XXX Software, Inc. as an unpublished work. All rights reserved. Running 我对提取“跑步

我想检查状态,如果状态不是“正在运行”,我想让我的脚本休眠5秒并增加计数器

可以使用mcstat检查电池的状态

14:24:25 # mcstat -n cell1
XXX Impact InfoStatus 9.5.00 (Build 241196604 - 15-Jan-2014) [l2]
Copyright 1998-2014 XXX Software, Inc. as an unpublished work.  All rights reserved.
Running
我对提取“跑步”感兴趣

我的剧本草稿

count=0
checker="false"

#take a nap before you work
sleep 2m

#grep for status string Running
status=`mcstat -n cell1| grep "Running"`


#lets count for 10 & keep checking for status
while [ $count -le 10 ]
do
    if [ ("$status" == "Running") ]; then
        checker=true
    else
        sleep 5s
        echo " waiting $count"
    fi
done
问题:
1.如何使用grep命令通过运行mcstat命令来查找字符串“Running”,并将其存储在变量中

也许这就是你想要的

status=`mcstat -n cellname | grep Running`
编辑:请注意,这些不是撇号,它们与~

或者,您可以:

status=`mcstat -n cellname | tail -1`
编辑:尝试以下操作:

while [ $count -le 10 ]
do
    status=`mcstat -n cell1 | tail -1`

    if [ "$status" == "Running" ]; then
        checker=true
        break
    else
        sleep 5s
        echo " waiting $count"
        count=$((count+1))
    fi
done
有关if/else的更多信息,请参阅本教程,我认为这就是您的问题所在:

是的,但是如果我的grep没有找到“Running”,它将抛出一个错误。它不应该抛出一个错误,它应该只返回一个空白字符串。此外,您还可以使用tail选项。哦,另外,在do中执行status=检查,否则您只会第一次检查…15:17:15#/test.sh-bash:./test.sh:第14行:意外标记附近的语法错误
“$status”'-bash:./test.sh:第14行:
if[(“$status”==“Running”);然后“我认为“$status”除了字符串值之外什么都没有,不是吗?我更新了我的答案以显示全部内容,我认为您的问题在于if/else中的()。