Command Line Processing / Pipes

The one liners mentioned below both work for me. I encounter the
problems only when I put the ruby in a script file.

···

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: 18 May 2006 16:06
To: ruby-talk ML
Subject: Re: Command Line Processing / Pipes

2006/5/18, Stuart Holden <Stuart.Holden@baring-asset.com>:

Hi,

I'm trying to create a script that that is flexible enough to accept
input that is read from a file whose filename is passed as a command
line argument, or from STDIN via a pipe.
(This is on a WinXP platform rather than Unix)

By way of a simple example, I want both of the following to produce

the

same results:

        1: myScript.rb *.txt
2: cat *.txt | myScript.rb

I have had some joy with the first approach...

                while line = ARGF.gets
                        #-- Do the interesting stuff here... or just
print the line.
                        print line
                end

However, I cannot get anything to work for the pipe approach. I

thought

that this would handle piped along STDIN

                while gets()
                        line = $_
                        print line
                end

but it chucks the following error:

                U:\dev\WM&CAPS\capsfiles>cat testfile.caps | caps.rb
                c:/scripts/caps.rb:1:in `gets': Bad file descriptor
(Errno::EBADF)
                        from c:/scripts/caps.rb:1

Works for me:

17:02:50 [~]: ruby -e 'while l=gets; p l; end' x
"foo\n"
17:03:07 [~]: cat x | ruby -e 'while l=gets; p l; end'
"foo\n"
17:03:12 [~]: uname -a
CYGWIN_NT-5.2-WOW64 PDBXPWSRK38 1.5.19(0.150/4/2) 2006-01-20 13:28 i686
Cygwin
17:03:16 [~]: ruby -v
ruby 1.8.4 (2005-12-24) [i386-cygwin]

I guess, you use the windows installer. I prefer the cygwin version as
it has better unix like behavior. Maybe that's an option for you too.

Kind regards

robert

--
Have a look: Robert K. | Flickr

<body>

<blockquote>
<font FACE="Arial,Arial" SIZE="1"><p ALIGN="JUSTIFY">This Email may contain confidential and privileged information and is intended for the use of the addressee(s) only. If you are not the intended recipient please notify the sender and delete the Email from your system. It should not be transmitted to any other person without the consent of the sender. Additional important notifications regarding Email transmissions from and to members of Baring Asset Management can be accessed at <a href="http://www.barings.com/email/index.hcst&quot;&gt;http://www.barings.com/email/index.hcst&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;&lt;/body&gt;
</p>
</font>
</blockquote>

<p>&nbsp;</p>
</body>
</html>