Shell 脚本监视特定文件的更改然后捕获它们

发布于 2024-11-30 01:22:33 字数 320 浏览 1 评论 0原文

我有一系列文件,只要其中一个文件更新,我就需要对其进行“cat”。例如,假设我们有 fileA.txt、fileB.txt 和 fileC.txt。当 fileA.txt 被修改并保存文件时,我需要运行一个脚本,除了其他任务外,

cat fileA.txt fileB.txt fileC.txt > combined.txt

我还知道它涉及观看文件,但我不确定如何处理这个问题。当我处理这些文件时,监视脚本将始终运行,然后在修改一组文件中的一个文件时执行“cat”命令。我使用的是 Mac,并且希望这是用 shell 编写的。

谢谢!

I have a series of files that I need to 'cat' whenever a one of the files gets updated. For example, say we have fileA.txt, fileB.txt, and fileC.txt. When fileA.txt is modified and the file is saved I need to run a script that among other tasks will

cat fileA.txt fileB.txt fileC.txt > combined.txt

I know this deals with watching files, but am unsure how to approach this. The watching script will always be running when I'm working on these files and then execute the 'cat' command when one of the files in a set of files is modified. I'm on a Mac and would prefer if this is written in shell.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

━╋う一瞬間旳綻放 2024-12-07 01:22:33

免责声明:这可能有点矫枉过正,但另一方面可能会更快地实现并且更稳定。

有一个名为 directory_watcher 的 ruby​​ 库,它可以监视目录的更改。

一个简单的脚本,例如

#!/usr/bin/env ruby

require 'rubygems'
require 'directory_watcher'

dw = DirectoryWatcher.new '.'
dw.add_observer do
  |*args| args.each do |event| 
    puts event
  end
end

dw.start
gets      # when the user hits "enter" the script will terminate
dw.stop

可以帮助您开始。这里修改(或删除或添加)的文件名只是打印到标准输出。


这是一个示例脚本,它将监视file1.txtfile2.txtfile3.txt。每当其中之一发生更改时,它会将它们连接到 files-combined.txt 中。

#!/usr/bin/env ruby

require 'rubygems'
require 'directory_watcher'

dw = DirectoryWatcher.new '.'
dw.interval = 1.0
dw.add_observer do |*args| 
  args.each do |event| 
    if /file\d/ =~ event.path
      `cat file1.txt file2.txt file3.txt > files-combined.txt`
      puts "#{Time.now.strftime("%I:%M:%S")} \
        Created files-combined.txt (since #{event.path} #{event.type})"
    end
  end
end

dw.start
gets      # when the user hits "enter" the script will terminate
dw.stop

输出如下:

$ ruby 7083085.rb 
08:55:47 Created files-combined.txt (since ./file3.txt added)
08:55:47 Created files-combined.txt (since ./file1.txt added)
08:55:47 Created files-combined.txt (since ./file2.txt added)
08:55:54 Created files-combined.txt (since ./file1.txt modified)
08:55:57 Created files-combined.txt (since ./file1.txt modified)

Disclaimer: This might be overkill, but on the other side maybe faster to implement and more stable.

There is a ruby library called directory_watcher, which can monitor directories for changes.

A simple script, such as

#!/usr/bin/env ruby

require 'rubygems'
require 'directory_watcher'

dw = DirectoryWatcher.new '.'
dw.add_observer do
  |*args| args.each do |event| 
    puts event
  end
end

dw.start
gets      # when the user hits "enter" the script will terminate
dw.stop

can get you started. Here the modified (or deleted or added) filenames just gets printed to stdout.


Here is an example script, that will watch for file1.txt, file2.txt, file3.txt. Whenever one of those is changed, it will concatenate them into files-combined.txt.

#!/usr/bin/env ruby

require 'rubygems'
require 'directory_watcher'

dw = DirectoryWatcher.new '.'
dw.interval = 1.0
dw.add_observer do |*args| 
  args.each do |event| 
    if /file\d/ =~ event.path
      `cat file1.txt file2.txt file3.txt > files-combined.txt`
      puts "#{Time.now.strftime("%I:%M:%S")} \
        Created files-combined.txt (since #{event.path} #{event.type})"
    end
  end
end

dw.start
gets      # when the user hits "enter" the script will terminate
dw.stop

Output would be like:

$ ruby 7083085.rb 
08:55:47 Created files-combined.txt (since ./file3.txt added)
08:55:47 Created files-combined.txt (since ./file1.txt added)
08:55:47 Created files-combined.txt (since ./file2.txt added)
08:55:54 Created files-combined.txt (since ./file1.txt modified)
08:55:57 Created files-combined.txt (since ./file1.txt modified)
半衬遮猫 2024-12-07 01:22:33

您也许可以利用尾巴。

tail -f f1.txt f2.txt >> c.txt

会将写入 f1.txt 和 f2.txt 的新行追加到 c.txt。为了避免 c.txt 被标题弄乱,请使用:

tail -qf f1.txt f2.txt >> c.txt

You might be able to utilize tail.

tail -f f1.txt f2.txt >> c.txt

will append new lines that are written to either f1.txt and f2.txt to c.txt. To avoid c.txt getting cluttered with headers use:

tail -qf f1.txt f2.txt >> c.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文