# Command Line Processing / Pipes

**URL:** <https://www.ruby-forum.com/t/command-line-processing-pipes/60910>\
**Category:** Ruby\
**Created:** [May 18, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/command-line-processing-pipes/60910 "2006-05-18T14:18:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stuart\_holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7993a0/32.png) [@stuart\_holden](https://www.ruby-forum.com/u/stuart_holden)\
**Post date:** [May 18, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/command-line-processing-pipes/60910/1 "2006-05-18T14:18:25Z")

</div>

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

It does work for STDIO that comes in from the console, but not the pipe.  
Oddly (for me), this code works when you pass the filename on the  
command line.  
Apologies for my newbie stupidity. I’ve had a good half hours google for  
help on this but haven’t had any luck.

Any help would be appreciated.

Thanks,  
Stuart H.

Senior Analyst Developer / IT Technical Services - Baring Asset  
Management (London)  
Tel Direct: +44 (0)20-7214 1932 Fax Direct: +44 (0)20-7214 1634  
E-mail: removed\_email\_address@domain.invalid Web:  
[http://www.baring-asset.com](http://www.baring-asset.com) [http://www.baring-asset.com/](http://www.baring-asset.com/)

> 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 [http://www.barings.com/email/index.hcst](http://www.barings.com/email/index.hcst)

&nbsp;

---

<div class="post-metadata">

**Author:** ![stuart\_holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7993a0/32.png) [@stuart\_holden](https://www.ruby-forum.com/u/stuart_holden)\
**Post date:** [May 18, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/command-line-processing-pipes/60910/2 "2006-05-18T15:23:05Z")

</div>

The following works on Win2k with ruby 1.8.2 (2004-12-25) [i386-mswin32]

type save\_sql\_env.txt | ruby -n -e “print”

I use ‘type’ as ‘cat’ is not a windows command in Win2k

---

<div class="post-metadata">

**Author:** ![stuart\_holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7993a0/32.png) [@stuart\_holden](https://www.ruby-forum.com/u/stuart_holden)\
**Post date:** [May 18, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/command-line-processing-pipes/60910/3 "2006-05-18T17:10:16Z")

</div>

2006/5/18, Stuart H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> 1: myScript.rb *.txt
> 
> ```
> 
> However, I cannot get anything to work for the pipe approach. I thought  
> 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
