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
Stefan Gremm wrote:
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
Am Dienstag, den 25.03.2008, 02:11 +0900 schrieb Rodrigo B.:
#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
Am Dienstag, den 25.03.2008, 03:12 +0900 schrieb Rodrigo B.:
did you change ?:
temp.each do |f|
with
$_.each do |f|
–
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
weird it is working for me…
touch 1
#create empty file
cat 1| 2.rb
: undefined method `each’ for nil:NilClass (NoMethodError)
cat 1| ruby -n 2.rb
#No errors
You need to specify the -n option.
#############
cat 2.rb
smtp_message = “”
$_.each do |f|
smtp_message += f
end
p smtp_message
Am Dienstag, den 25.03.2008, 08:50 +0900 schrieb Eric H.:
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
On Mar 24, 2008, at 07:51 AM, Stefan Gremm wrote:
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