Pipe command to script

Hello to all,

Having a ruby script that works when an argument is given in command line in this way:

ruby script.rb input_to_ruby

To accept arguments as input, inside the ruby script has

File.open(ARGV[0])

input_to_ruby is generated by another command, so I need to create first input_to_ruby in order to give as argument to script.rb, in this way.

ls -laD file1 > input_to_ruby

How can I pipe the output of "ls" to avoid create first the input_to_ruby and do it directly? something like:

ls -laD file1 | script.rb

Thanks in advance

Hello,

You can do it with xargs command:

ls -laD file1 | xargs script.rb

see man xargs for more informations.

Regards.

Hello

I'm not sure if some parameter is missing in xarg command, I've tried both ways below and is not working.

ls -laD file1 | xargs script.rb
xargs: script.rb: No such file or directory

ls -laD file1 | xargs script.rb
script1F.rb:3:in `initialize': No such file or directory - (Errno::ENOENT)
        from script.rb:3:in `open'
        from script.rb:3:in `<main>'

Try and see if this works:

ls -laD file1 | script.rb -

Sorry,
I have not read correctly the problem.
If this syntax is available in your shell (e.g: available in bash):

script.rb <(ls -alD file)

Beware: no space between '<' and '('
Regards.

Hi Ophiuchus ,
Expanding on what disedorgue said, if:

ruby script.rb <(ls -laD file1)

won't work in your shell (recent versions of both bash and ksh support this syntax), some systems have a pathname in /dev or /proc that is a name for the process's file descriptor 0 (AKA standard input). For example, on Mac OS X, the following should work for you:

ls -laD file1 | ruby script.rb /dev/stdin

If neither of these work for you and you can't find a name for standard input in the filesystem hierarchy on your system, we might be able to help you find a way to make it work if you'd tell us what operating system and shell you are using.

Hello disedorgue and scrutinizer,

Thank you for the help.

I'm using cygwin, maybe it has some limitations but doesn't work with

ruby script.rb <(ls -laD file1)
nor
ls -laD file1 | script.rb -

Hello Don Cragun,

Thanks for share another option, works in two forms below:

ls -laD file1 | ruby script.rb /dev/stdin
and
ls -laD file1 | ruby script.rb

Many thanks all 3 for the time and willingness to help and share knowledge.

Best regards.