2014-09-29 22 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

相關問題