【问题标题】:grep status and loop it until the count condition in shellgrep status 并循环它直到shell中的计数条件
【发布时间】:2014-09-29 19:52:06
【问题描述】:

我会检查状态,如果状态不是“正在运行”,我希望我的脚本休眠 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”并将其存储在变量中。

【问题讨论】:

    标签: shell scripting grep


    【解决方案1】:

    也许这就是你想要的?

    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 的更多信息,请参阅本教程,我认为这就是您的问题所在: http://www.dreamsyssoft.com/unix-shell-scripting/ifelse-tutorial.php

    【讨论】:

    • 是的,但是如果我的 grep 没有找到“正在运行”,它会抛出一个错误。
    • 它不应该抛出错误,它应该只返回一个空白字符串。您也可以使用 tail 选项。
    • 哦,还有,在do里面做status=检查,不然你只检查第一次...
    • 15:17:15 # . ./test.sh -bash: ./test.sh: 第 14 行:意外标记附近的语法错误 "$status"' -bash: ./test.sh: line 14: if [ ("$status" == "Running") ];然后'我认为“$status”只是字符串值,不是吗?
    • 我更新了我的答案以显示整个事情,我认为你的问题出在 if/else 与 () 那里。
    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多