From: matz@... (Yukihiro Matsumoto) Date: 2002-02-19T12:25:11+09:00 Subject: Re: expect.rb vs Expect (Tcl)... Hi, In message "expect.rb vs Expect (Tcl)..." on 02/02/19, Hugh Sasse Staff Elec Eng writes: |Are there plans to improve this funtionality to allow one to expect on |many patterns, performing actions as appropriate? Don't expect too much for 36 lines of code. Here's my own version of expecter, that waits for many patterns. class Expecter def initialize(timeout=nil, dynamic=nil) @timeout=timeout @action= {} @dynamic=dynamic end def on(pattern, action=lambda) @action[pattern] = action end def wait(on) keys = @action.keys while TRUE # fds = select([on], nil, nil, @timeout) fds = select([on], nil, nil) unless fds if @action['TIMEOUT'] @action['TIMEOUT'].call end return end break if fds.size == 0 or fds[0].size == 0 line = on.gets break unless line print line for key in keys if line =~ key @action[key].call(line) end end keys = @action.keys if @dynamic end if @action['EOF'] @action['EOF'].call end end def expect(on) fds = select([on], nil, nil, @timeout) unless fds if @action['TIMEOUT'] @action['TIMEOUT'].call end return end return if fds.size == 0 or fds[0].size == 0 line = on.gets unless line if @action['EOF'] @action['EOF'].call end return end print line for key in @action.keys if line =~ key @action[key].call(line) end end end end Usage: expect = Expecter.new expect.on(/NO CARRIER/) do exit 1 end expect.on(/Exit/) do exit 1 end expect.on(/Failed/) do exit 1 end expect.on(/[^n]REDIAL ERROR/) do exit 1 end f = open("|tail -n 0 -f /var/log/messages") dial expect.wait(f)