他のプロセスに出力がない場合は、bashコマンドを繰り返します。

他のプロセスに出力がない場合は、bashコマンドを繰り返します。

私は走っているfs_usage私のファイルシステムのオブジェクトへのアクセスを検出します。

sudo fs_usage -w | grep -E 'object'

touch5秒以内に上記のコマンドの新しい出力がない限り、このオブジェクトに対して5秒ごとにコマンドを実行したいと思います。

ベストアンサー1

sudo fs_usage -w | while true; do
    if read -rt5 && [[ $REPLY =~ objectpattern ]]; then
        # Some output happened in the last 5 seconds that matches object pattern
        :
    else 
        touch objectfile
    fi
done

もちろん使用することは、read -tいくつかの出力が可能であることを意味します。いいえmatch objectpattern;これが発生すると、ファイルがタッチされます。これを避けたいなら、もう少し洗練されなければなりません。

timeout=5
sudo fs_usage -w | while true; do
    (( mark = SECONDS + timeout ))
    if !read -rt$timeout; then
        touch objectfile
        timeout=5
    elif ![[ $REPLY =~ objectpattern ]]; then
        # Some output happened within timeout seconds that does _not_ match.
        # Reduce timeout by the elapsed time.
        (( timeout = mark - SECONDS ))
        if (( timeout < 1 )); then
            touch objectfile
            timeout=5
        fi
    else
        timeout=5
    fi
done

おすすめ記事