开发者

Delayed Monitoring in Bash

开发者 https://www.devze.com 2023-02-19 01:04 出处:网络
What\'s the best way to \"wait\" in a Bash script, until the result of a command contains a specific pattern?

What's the best way to "wait" in a Bash script, until the result of a command contains a specific pattern?

I've written a 开发者_JAVA百科simple script to repair a RAID array, and now I want the script to wait until the command cat /proc/mdstat reports that the rebuilding of the array is complete. I want to wait, because afterwards, I need to install Grub on the new device via sudo grub-install /dev/sd*


Something like

#! /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

Do you need an explanation?

I hope this helps.


(there are more succinct ways of doing some of this for just bash, but I tend to write in portable SH when doing things like this:)

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
0

精彩评论

暂无评论...
验证码 换一张
取 消