0

短篇小说:在我的 Linux 桌面上,我希望在/dev创建或删除下的节点时收到通知(知道插入某些设备时创建了哪些节点非常有用)。我为此写了两个幼稚的脚本:

第一个通过以下方式将这些更改写入日志文件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命令)并显示通知的第二个脚本:

#!/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 毫秒)以获得更多条目,并且只有在最后一次收到条目后超时后,才会使用 .cry 收集条目notify-send

我不是经验丰富的 bash 程序员(和 linux 用户),所以如果有人给我一些关于如何正确实现它的线索,我会很高兴。

4

1 回答 1

1

我对 bash 不太熟悉,但我认为您可以将 tail 的输出提供给 while 循环,如下面的 bash 脚本:

#/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)。超时以秒为单位,使用 0.3 表示 300 毫秒。

(编辑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 回答