Q:

Shell script that watches for changes to specific files then cats them

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! 

A:

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)

