# How to redirect $stdin to read from a string?

**URL:** <https://www.ruby-forum.com/t/how-to-redirect-stdin-to-read-from-a-string/139900>\
**Category:** Ruby\
**Created:** [June 8, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/how-to-redirect-stdin-to-read-from-a-string/139900 "2008-06-08T21:17:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASLester\_Buck\_III](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@ASLester\_Buck\_III](https://www.ruby-forum.com/u/ASLester_Buck_III)\
**Post date:** [June 8, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/how-to-redirect-stdin-to-read-from-a-string/139900/1 "2008-06-08T21:17:35Z")

</div>

Hi,

I want to send a Windows popup from a Linux machine. smbclient is  
the standard utility, and it will accept a file on standard  
input to avoid terminal interaction, viz.

```
smbclient -M netbiosname < /tmp/message.text

```

From my reading of the Ruby Cookbook, recipes 6.15 and 6.16, this  
“should” work:

=========  
#!/usr/bin/ruby -w

require ‘stringio’

$stdin = StringIO.new “This is the test message”

# system(“smbclient -M netbiosname”)

Alas, it ignores the input message and prompts for terminal input  
terminated by ^D.

Could someone please show me the correct code to use in a Ruby  
script to send a string to a Windows popup?

Thanks a lot!

Lester

---

<div class="post-metadata">

**Author:** ![ASLester\_Buck\_III](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@ASLester\_Buck\_III](https://www.ruby-forum.com/u/ASLester_Buck_III)\
**Post date:** [June 8, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/how-to-redirect-stdin-to-read-from-a-string/139900/2 "2008-06-08T21:22:53Z")

</div>

On Jun 8, 2008, at 12:13 PM, A. Lester Buck III wrote:

> =========  
> terminated by ^D.
> 
> Could someone please show me the correct code to use in a Ruby  
> script to send a string to a Windows popup?

You’ll have to use #reopen with a real file instead. Assigning to  
$stdin only affects ruby’s interpretation of standard input, not the C  
standard input.

---

<div class="post-metadata">

**Author:** ![ASLester\_Buck\_III](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@ASLester\_Buck\_III](https://www.ruby-forum.com/u/ASLester_Buck_III)\
**Post date:** [June 8, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/how-to-redirect-stdin-to-read-from-a-string/139900/3 "2008-06-08T21:45:14Z")

</div>

On Sunday 08 June 2008 14:13:54 A. Lester Buck III wrote:

> require ‘stringio’
> 
> $stdin = StringIO.new “This is the test message”
> 
> system(“smbclient -M netbiosname”)

That only affects the stdin variable – and if it did do what you’re  
thinking,  
that’s probably not a good idea anyway. You probably don’t want to set  
stdout  
for the entire rest of your program.

What you probably want is something like popen:

def send\_message message, netbiosname  
IO.popen “smbclient -M #{netbiosname}”, ‘w’ do |io|  
message.each\_line do |line|  
io.write line  
end  
end  
raise ‘smbclient failed’ unless $?.success?  
end

I’m using each\_line because it exists on both String and IO – so you  
don’t  
have to wrap the message in a StringIO. It won’t work very well for  
large  
chunks of binary, but I don’t think you’ll be sending those…

---

<div class="post-metadata">

**Author:** ![ASLester\_Buck\_III](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@ASLester\_Buck\_III](https://www.ruby-forum.com/u/ASLester_Buck_III)\
**Post date:** [June 8, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/how-to-redirect-stdin-to-read-from-a-string/139900/4 "2008-06-08T23:02:16Z")

</div>

Thanks for the pointers! I’m trying to avoid writing a temporary file  
and then feeding that back to the command.

> def send\_message message, netbiosname  
> IO.popen “smbclient -M #{netbiosname}”, ‘w’ do |io|  
> message.each\_line do |line|  
> io.write line  
> end  
> end  
> raise ‘smbclient failed’ unless $?.success?  
> end

This works great. My script is a caller id popup/logger on Linux. I  
thought I would add notifiers to the other systems on my KVM switch so I  
don’t have to switch back and forth to see if I need to pick up. This  
code runs inside a forked process which is then thrown away, so  
redirecting all of $stdin wouldn’t have been a problem, but this avoids  
that issue, too.

I really appreciate your help!

Lester
