2014-09-29 26 views
0

我想检查状态,如果状态不是“正在运行”,会让我的脚本睡眠5秒并增加计数器。细胞的grep状态并循环它,直到shell中的计数条件

状态可以用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”并将其存储在一个变量中。

回答

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

+0

是的,但如果我的grep没有找到“正在运行”,它会抛出一个错误。 – 2014-09-29 20:15:01

+0

它不应该抛出一个错误,它应该只是返回一个空白字符串。你也可以使用尾部选项。 – 2014-09-29 20:15:48

+0

哦,另外,做的状态=检查里面的做,否则你只检查第一次... – 2014-09-29 20:17:42

相关问题