2011-03-23 67 views
0

在Bash脚本中“等待”,直到命令的结果包含特定模式为止的最佳方式是什么?Bash中的延迟监视

我已经写了一个简单的脚本来修复RAID阵列,现在我希望脚本等到命令cat /proc/mdstat报告阵列重建完成。我想等一下,因为之后我需要在新设备上安装Grub,通过sudo grub-install /dev/sd*

回答

2

喜欢的东西

#! /bin/bash 
doneString="RAIDFix Completed successfully" 

until ${mdstat_done:-false} ; do 
    if grep "${doneString:?}" /proc/mdstat > /dev/null ; then 
     sudo grub-install /dev/sd* 
     mdstat_done=true 
    else 
     sleep ${sleepSecs:-60} 
    fi 
done 

你需要一个解释?

我希望这会有所帮助。

2

这:)

weredone=0 
while test $weredone = 0 ; do 
    # this is not actually what you want to grep for, but you get the idea: 
    grep complete /proc/mdstat 
    if test $? = 0 ; then 
     weredone=1 
    else 
     sleep 5 
    fi 
done