gpt4 book ai didi

bash - 如何在文件随机更改时连续处理两个文件的最后一行?

转载 作者:行者123 更新时间:2023-11-29 09:27:34 27 4
gpt4 key购买 nike

我有以下简单的片段:

#!/bin/bash

tail -f "data/top.right.log" | while read val1
do
val2=$(tail -n 1 "data/top.left.log")
echo $(echo "$val1 - $val2" | bc)
done

top.left.logtop.right.log 是一些其他进程不断写入的文件。 bash 脚本简单地减去两个文件的最后几行并显示结果。

我想让脚本更有效率。在伪代码中我想这样做:

#!/bin/bash

magiccommand "data/top.right.log" "data/top.left.log" | while read val1 val2
do
echo $(echo "$val1 - $val2" | bc)
done

这样,每当 top.left.log OR top.right.log 更改时,都会调用 echo 命令。

我已经尝试过 StackOverflow 的各种片段,但它们通常依赖于文件不会更改或两个文件包含相同数量的行的事实,这不是我的情况。

最佳答案

如果你有inotify-tools,你可以使用下面的命令:

inotifywait -q -e modify file1 file2

描述:

   inotifywait  efficiently waits for changes to files using Linux's inotify(7) interface.
It is suitable for waiting for changes to files from shell scripts.
It can either exit once an event occurs, or continually execute and output events as they occur.

一个例子:

while : ;
do
inotifywait -q -e modify file1 file2
echo `tail -n1 file1`
echo `tail -n1 file2`
done

关于bash - 如何在文件随机更改时连续处理两个文件的最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com