Pipe command to script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pipe command to script
# 1  
Old 09-08-2013
Pipe command to script

Hello to all,

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

To accept arguments as input, inside the ruby script has
Code:
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.
Code:
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:
Code:
ls -laD file1 | script.rb

Thanks in advance
# 2  
Old 09-08-2013
Hello,

You can do it with xargs command:

Code:
ls -laD file1 | xargs script.rb

see man xargs for more informations.

Regards.
# 3  
Old 09-08-2013
Hello

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

Code:
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>'

# 4  
Old 09-08-2013
Try and see if this works:
Code:
ls -laD file1 | script.rb -


Last edited by Scrutinizer; 09-08-2013 at 08:04 AM..
# 5  
Old 09-08-2013
Sorry,
I have not read correctly the problem.
If this syntax is available in your shell (e.g: available in bash):
Code:
script.rb <(ls -alD file)

Beware: no space between '<' and '('
Regards.
# 6  
Old 09-08-2013
Hi Ophiuchus ,
Expanding on what disedorgue said, if:
Code:
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:
Code:
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.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-08-2013
Hello disedorgue and scrutinizer,

Thank you for the help.

I'm using cygwin, maybe it has some limitations but doesn't work with
Code:
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in using cut command with pipe as a delimiter while using in a script

There is a text file in my project named as "mom.txt" in which i want to have contents like.................. LSCRM(Application Name): 1: This is my first application. 2: Today we did shell scripting automation for this app. 3: It was really a good fun in doing so. 4: Really good.| (Here i... (7 Replies)
Discussion started by: Abhijeet Anand
7 Replies

2. Shell Programming and Scripting

How to pipe command output to shell script?

Hi Team, Need a help on how to pipe a command out put to a shell script. My shell script looks like below. cat shell_script #!/usr/bin/ksh input =$@ echo " we are inside the shell script" echo " here are the input parameters" .......................... .................. ... (11 Replies)
Discussion started by: gvkumar25
11 Replies

3. Shell Programming and Scripting

Fix pipe command

Trying to get the below command to work which it does, however each time, regardless of host being up or not it always loops back to the start of the function where I need it to stop if the host is up and continue with teh rest of teh function. echo -ne "Remote/local host address. Example... (2 Replies)
Discussion started by: 3therk1ll
2 Replies

4. Shell Programming and Scripting

pipe in command

Hello, I try to concatenate a command to execute. Sadly it throws an error. #!/bin/bash cd / cmd="find -name *.txt | awk '{ printf "FILE: "$1; system("less "$1);}' | egrep 'FILE:|$1'" echo "1." $($cmd) echo "2." $("$cmd") echo "3." `$cmd` echo "4." `"$cmd"`1.&3. 'find: paths must... (2 Replies)
Discussion started by: daWonderer
2 Replies

5. Shell Programming and Scripting

pipe grep command

Hi all, Can someone help me with the following problem. I am executing the following command: (search for occurences of 'error' in files that match cl-*.log expression) > grep -cw -i --max-count=1 'error' cl-*.log this command outputs: cl-apache.log:1 cl-apache_error.log:1... (3 Replies)
Discussion started by: epro66
3 Replies

6. UNIX for Advanced & Expert Users

unix command pipe

I am pretty new to UNIX. My client has a requirement where in a directory we have some files with somewhat similar name like test_XX.txt, test_XY.txt, test_XZ.txt, test_ZZ.txt, test_ZY.txt, test_ZX.txt, test_YY.txt......Out of these files few files have 0 bytes. Is there a way where we can go... (7 Replies)
Discussion started by: RubinPat
7 Replies

7. UNIX for Dummies Questions & Answers

pipe output to script as command line argument

i want to redirect output of one command as the command line argument of another script for example, say i would run this command: find . -xdev -type f -size +4096 -exec ls -al {} \; i wan to be able to do something like: echo +4096 | find . -xdev -type f -size ****** -exec... (3 Replies)
Discussion started by: IMTheNachoMan
3 Replies

8. UNIX for Dummies Questions & Answers

How can I use pipe command ?

Hi My friends I have used this command to find files are modified within the past 24 hours and then many files are shown but I want transfer all these files to special directory by using pipe . can any one tell me what is the next step ? (11 Replies)
Discussion started by: bintaleb
11 Replies

9. UNIX for Dummies Questions & Answers

pipe command

current dir : /home/sales ls -l abc.txt 17th aug bcd .txt 16t oct ------- ------ Total files : 100 if i want to move only those files dated 17 aug into another sub directory /home/sales/texas how do i pipe the result of 'ls' command to a 'mv' command (1 Reply)
Discussion started by: zomboo
1 Replies

10. UNIX for Advanced & Expert Users

How to pipe command

Hi All, I want to create a command that executes a text editor with the most recent file in the current current directory. So a good start to achieve this is : ls -lrt | cut -c55- | tail -1 which provides the name of the most recent file in a directory The problem is to pipe the... (4 Replies)
Discussion started by: anonymous.nico
4 Replies
Login or Register to Ask a Question