reopening stdin

S

Sea&Gull

Hi!

I have a script.rb which reads some data from stdin.
Something like this:

bash$ cat file_name | script.rb

In script.rb I have:

#--------------------
fi = $stdin

fi.each_line do |l|
#...
end

--------------------

After that script.rb needs to communicate with a user interactively:
ask what to do and read the answer from... stdin. Something like:

#-------------------

print "\nYes/No: "
answer = $stdin.gets

#-------------------

Here I had a problem - I can't read user's answer.
I guess I need somehow to reopen stdin before calling
$stdin.gets but have not found how to do it correctly.

Could you please give me a hint?

Thanks a lot!
 
A

Ara.T.Howard

Hi!

I have a script.rb which reads some data from stdin. Something like this:

bash$ cat file_name | script.rb

In script.rb I have:

#--------------------
fi = $stdin

fi.each_line do |l|
#...
end

--------------------

After that script.rb needs to communicate with a user interactively: ask
what to do and read the answer from... stdin. Something like:

#-------------------

print "\nYes/No: "
answer = $stdin.gets

#-------------------

Here I had a problem - I can't read user's answer. I guess I need somehow
to reopen stdin before calling $stdin.gets but have not found how to do it
correctly.

Could you please give me a hint?


well, here you setup stdin of the script.rb to be a pipe and therefore it has
no controlling terminal:

bash$ cat file_name | script.rb

so, even if you reopen it it will still be a pipe - you cannot get back at the
terminal. i think what you are saying is that you want to open the console
after reading stdin. so maybe:

harp:~ > cat a.rb
print STDIN.read
STDIN.reopen '/dev/tty'
print STDIN.read

harp:~ > printf "42\n" | ruby a.rb
42
then i type 42 followed by ctrl-d
then i type 42 followed by ctrl-d

hth.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
S

Sea&Gull

Ara.T.Howard said:
well, here you setup stdin of the script.rb to be a pipe and therefore
it has
no controlling terminal:

bash$ cat file_name | script.rb

so, even if you reopen it it will still be a pipe - you cannot get back
at the
terminal. i think what you are saying is that you want to open the console
after reading stdin. so maybe:

harp:~ > cat a.rb
print STDIN.read
STDIN.reopen '/dev/tty'
print STDIN.read

harp:~ > printf "42\n" | ruby a.rb
42
then i type 42 followed by ctrl-d
then i type 42 followed by ctrl-d

hth.

-a

Yes, it was exactly what I was asking
about (with slightly wrong formulation :) )

Thanks, you helped me a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,513
Latest member
JeremyLabo

Latest Threads

Top