KennyC
KennyC

Reputation: 961

How to redirect input to the Ruby command-line as argument

I'd like to populate a Ruby script's ARGV with input from the command-line using redirection:

ruby myscript.rb < my_cmdline_args.txt

This does not seem to work. My script complains that ARGV is empty.

Upvotes: 0

Views: 758

Answers (3)

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369614

It's really unclear what you are looking for, since you are both talking about using the contents of a file as arguments, as well as redirection, i.e. using the contents of a file as input.

However, I do believe that something like this is what you want:

FOR /F %i IN (my_cmdline_args.txt) DO ruby myscript.rb %i

Upvotes: 0

Alex
Alex

Reputation: 1442

You can do this with xargs:

cat my_cmdline_args.txt | xargs ruby myscript.rb

More info: http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs

Upvotes: 1

rorra
rorra

Reputation: 9693

On your script you can use ARGF to read from the linux pipes, like:

#!/usr/bin/env ruby
puts ARGF.read

try sending a file or some input and it should work, you can do

ruby myscript.rb < my_cmdline_args.txt

and it will echo the content or

cat my_cmdline_args.txt | ruby myscript.rb

and it will read the contents the same way

Upvotes: 1

Related Questions