tags:

views:

53

answers:

4

Hello everyone,

I am new to Linux. And I am using Red Hat Enterprise Version 5. There is a ruby program which use standard input as its input (e.g. the Ruby program process input from standard input). I think standard input should be keyboard, correct?

So, I think other kinds of input (non-standard input) should not work (i.e. the ruby program should not be able to read input from such non-standard input), but actually I have tried using pipe works, I am so confused because I think pipe should be some other kinds of input -- other than standard input, why it could work? i.e. put text "123" in abc.txt with pipe, could achieve the same result as using keyboard as input to type "123" for the ruby program.

Here is the sample which works and makes me confused,

cat abc.txt | ~/test/rubysrc/foo.rb

thanks in advance, George

+2  A: 

Pipe works as a "redirector" from STDOUT (which does cat abc.txt) to STDIN (which accepts the foo.rb script.

IMHO this belongs on superuser.com

Eimantas
+2  A: 

Actually, the pipe connects the left process's stdout to the right process's stdin, so it makes sense that this works. See also http://en.wikipedia.org/wiki/Pipeline_%28Unix%29.

(And yes, this should probably go to superuser.com.)

djc
So, the right process cannot process keyboard input if connected by pipe?
George2
+2  A: 

Use $stdin.isatty if you want to try to prevent non-terminal input. Do note that there are ways around this if the user is determined enough.

Ignacio Vazquez-Abrams
So, the right process of the process cannot process keyboard input if connected by pipe?
George2
Not via stdin. It can still open up `/dev/tty` though, and get input from there.
Ignacio Vazquez-Abrams
Thanks, question answered!
George2
+2  A: 

Pipes work by connecting standard input and output of one process to other processes, simulating a user typing on his keyboard. This allows processes to be connected together without having to know about each other, or used directly by the user from his keyboard.

Wikipedia has a nice writeup.

dsolimano