linux - bash 读取 : group read results by timeout

标签 linux bash inotifywait

短篇小说:在我的 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

第二个脚本监控该日志文件(使用 bash read 命令)并显示通知:

#!/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 用户),所以如果有人给我一些关于如何正确实现这个的线索,我会很高兴。

最佳答案

我对 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

关于linux - bash 读取 : group read results by timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465166/

相关文章:

linux - Tomcat8 正在运行,但状态为 Tomcat servlet 引擎未运行

c++ - 使用 GCC 构建 Linux 可执行文件

regex - 在 bash 中查找与多个模式匹配的文件

linux - 通过 inotifywait 管理 Vhost 的访问

linux - lpr: 错误 - 未知选项 "V"

linux - 如何通过命令行在 VPS 上安装 Firefox?

linux - 在 Linux Mint 上使用 play 时出错

python - 如何知道输出是否进入终端?

linux - 我可以在没有 cron 或 incron 的 Linux 服务器上连续运行 inotifywait

bash - 在运行脚本之前使用 inotifywait (或替代方案)等待 rsync 传输完成?