0

短い話: 私の Linux デスクトップでは、ノード/devが作成または削除されるたびに通知が必要です (デバイスを接続したときにどのノードが作成されるかを知ることは非常に便利です)。そのための単純なスクリプトを 2 つ書きました。

最初のものは、次の方法でこれらの変更をログ ファイルに書き込みますinotifywait

#!/bin/sh

inotifywait -m -e create,delete --timefmt '%Y.%m.%d-%H:%M:%S' --format '[%T] %e %w%f' /dev > /var/log/devdir_changes

結果のログ ファイルは次のようになります。

[2014.08.19-01:32:51] CREATE /dev/vcs63
[2014.08.19-01:32:51] CREATE /dev/vcsa63

そして、そのログ ファイルを (bashreadコマンドで) 監視し、通知を表示する 2 番目のスクリプト:

#!/bin/sh

while true; do

   # -----------------------------------------------------------------------------------
   # Now, listen for new messages
   echo "listening for new messages.."

   tail -f -n 0 /var/log/devdir_changes | \
      while read time type message; do
         notify-send "$type" "$message"
      done

      echo "restarting in 5 seconds.."
      sleep 5
      echo "restarting.."
done

echo "exiting."

機能しますが、予想どおり、作成/削除されたノードごとに専用の通知バルーンがあります。通常、単一の USB デバイスを接続すると複数のノードが存在しますが、非常に多くのノードが存在する場合もあります。そのため、新しいエントリが検出されたら、しばらく (たとえば 200 ~ 300 ミリ秒) 待機してエントリを追加し、最後にエントリを受信して​​からタイムアウトした後にのみ、収集されたエントリをnotify-send.

私は経験豊富な bash プログラマー (および Linux ユーザー) ではないので、誰かがこれを正しく実装する方法について手がかりを教えてくれたらうれしいです。

4

1 に答える 1

1

私は bash の経験があまりありませんが、次の bash スクリプトのように、tail の出力を while ループにフィードできると思います。

#/bin/bash

# maximum time between records to be grouped
# in seconds, e.g. "0.300" for 300ms
#TIMEOUT=0.300
TIMEOUT=3.1

# maximum number of records to be grouped
LIMIT=100

LINE_BREAK=$'\n'

# tail -f -n 0 /var/log/devdir_changes | \
while true
do
    declare -a times types messages

    # wait for, read and store first record
    read time type message
    times[0]="$time"
    types[0]="$type"
    messages[0]="$message"
    size=1

    # wait for more records to appear within timeout
    while [ $size -lt "$LIMIT" ]
    do
        read -t "$TIMEOUT" time type message || break
        times[$size]="$time"
        types[$size]="$type"
        messages[$size]="$message"
        size=$((${size} + 1))
    done

    # build message from record group
    message="${types[0]} ${messages[0]}"
    i=1
    while [ $i -lt $size ]
    do
        message="$message$LINE_BREAK${types[$i]} ${messages[$i]}"
        i=$((i + 1))
    done

    # send message as notification
    echo "$message"
    # notify-send "$message"
done

キーは、読み取りの呼び出しでタイムアウト (-t 3.1) を使用し、タイムアウトに達するか、バッファーが「いっぱいになる」まで (例では 100 を制限)、入力を (配列で) バッファリングします。タイムアウトは秒単位で指定され、300 ミリ秒には 0.3 を使用します。

(編集 1: いくつかのコメント、最初のレコードのタイムアウトなし)


編集 2:利用可能時間による行のグループ化をより再利用可能にするために、関数を使用できます。

# group lines which get available at the same time
#
# This can be used to group lines from asynchronous input
# according to (sufficiently large) time gaps between lines.
#
# $1 = max seconds to wait for another line; default: 1.5
# $2 = max number of lines to read; default: 10
# $3 = group terminator to use; default: $'\0'
function time_group_lines() {
    local timeout="${1:-1.5}"
    local limit="${2:-10}"
    local terminator="${3}"
    local line
    while true ; do
        read line || return 0
        echo "$line"
        size=1
        while [ $size -lt "$limit" ] ; do
            read -t "$timeout" line || break
            echo "$line"
            size=$(($size + 1))
        done
        if [ -z "$terminator" ]
        then echo -n -e "\x00"
        else echo -n "$terminator"
        fi
    done
}

# tail -f -n 0 /var/log/devdir_changes | \
# sed 's/^[^ ]* //' \
time_group_lines "$TIMEOUT" "$LIMIT" | \
while true ; do
    read -d $'\0' group || break
    # notify-send "$group"
    echo -e "$group"
done
于 2014-08-23T21:30:57.953 に答える