$stdin

S

Stefan Gremm

Hello,

first excuse me my bad english.

i have write a little send_mail script. now i pipe the messagetext from
another program, like cat /var/mailtext| send-mail.rb

the program source:

smtp_message = ""

temp = $stdin

temp.each do |f|
smtp_message += f
end

this is runing very well, but when no text is in the pipe, the program
is waiting for input.

my question is, how can i ask, if text in $stdin, without the program is
waitung for input.

ciao

Stefan
 
R

Rodrigo Bermejo

Stefan said:
Hello,

first excuse me my bad english.

i have write a little send_mail script. now i pipe the messagetext from
another program, like cat /var/mailtext| send-mail.rb

the program source:

smtp_message = ""

temp = $stdin

temp.each do |f|
smtp_message += f
end

this is runing very well, but when no text is in the pipe, the program
is waiting for input.

my question is, how can i ask, if text in $stdin, without the program is
waitung for input.

ciao

Stefan


Try the -n option
-n assume 'while gets(); ... end' loop around your
script


#code
smtp_message = ""
$_.each do |f|
smtp_message += f
end
#endcode


cat /var/mailtext| ruby -n send-mail.rb

-rb
 
S

Stefan Gremm

Am Dienstag, den 25.03.2008, 02:11 +0900 schrieb Rodrigo Bermejo:
Try the -n option
script


#code
smtp_message = ""
$_.each do |f|
smtp_message += f
end
#endcode


cat /var/mailtext| ruby -n send-mail.rb

thanks for the answer, but when i start my script is coming the folowing
error

undefined method `each' for nil:NilClass (NoMethodError)

and my command parser brings failure

ciao

Stefan
 
R

Rodrigo Bermejo

Stefan said:
Am Dienstag, den 25.03.2008, 02:11 +0900 schrieb Rodrigo Bermejo:

thanks for the answer, but when i start my script is coming the folowing
error

undefined method `each' for nil:NilClass (NoMethodError)

and my command parser brings failure

ciao

Stefan

did you change ?:
 
S

Stefan Gremm

Am Dienstag, den 25.03.2008, 03:12 +0900 schrieb Rodrigo Bermejo:
did you change ?:
--

yes, now i have
$stdin.each do |f| unless $stdin == nil
smtp_message += f
end
end

but when i have nothing in $stdin, the program is waiting

Ciao

Stefan
 
E

Eric Hodel

my question is, how can i ask, if text in $stdin, without the
program is
waitung for input.

Here's one way, but it isn't foolproof.

$ ruby -e 'p $stdin.tty?'
true
$ ruby -e 'p $stdin.tty?' < /dev/null
false
$ cat /dev/null | ruby -e 'p $stdin.tty?'
false
 
S

Stefan Gremm

Am Dienstag, den 25.03.2008, 08:50 +0900 schrieb Eric Hodel:
Here's one way, but it isn't foolproof.

$ ruby -e 'p $stdin.tty?'
true
$ ruby -e 'p $stdin.tty?' < /dev/null
false
$ cat /dev/null | ruby -e 'p $stdin.tty?'
false

eric, thank you for your answer.

But, when starting the program without pipe, the result is true too

Mhmm, my program becoms the messagetext over pipe or over
commandparameter ,if over pipe, all is good, if over parameter, the
program hangs.

Ciao

Stefan
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top