# Executing shell commands from Ruby... again

**URL:** <https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074>\
**Category:** Ruby\
**Created:** [March 16, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074 "2007-03-16T21:48:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/1 "2007-03-16T21:48:19Z")

</div>

I’m not only very new to Ruby, but also to server side programming in  
general, so I’m having a hard time finding a good place to look for  
solutions.

I’ve seen a number of posts regarding the execution of shell commands  
from Ruby / Rails, but I can’t figure out a way to call a shell process  
and retain a reference to it so that I can execute successive commands.  
I need to do this on a Windows machine, so a typical batch file might be  
constructed like this:

echo off  
cd path\executabledir  
 -S

where anything in \<\> needs to be dynamically populated. I’m able to  
execute a batch file with the information hard-coded using  
IO.popen(“batchfile”), but I need to be able to manipulate the shell  
commands before running them.

If anyone knows of any fitting resources that a newbie to this entire  
concept could understand, please let me know.

Thanks.

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/2 "2007-03-16T22:00:30Z")

</div>

Kevin S. schrieb:

> I need to do this on a Windows machine, so a typical batch file might be  
> constructed like this:
> 
> echo off  
> cd path\executabledir  
> -S
> 
> where anything in \<\> needs to be dynamically populated.  
> How about this:

# – BEGIN OF EXAMPLE

# setting command, param, input, output

# …

cmd = \<\<END\_OF\_STRING  
echo off  
cd path\executabledir  
#{command} -S #{param} #{input} #{output}  
END\_OF\_STRING

system(cmd)

# – END OF EXAMPLE

regards  
Jan

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/3 "2007-03-16T22:10:38Z")

</div>

On 3/16/07, Kevin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can’t figure out a way to call a shell process  
> and retain a reference to it so that I can execute successive commands.  
> I need to do this on a Windows machine,

While trivial on \*nix, this task is not as easy as it should be on  
Windows.  
I am specifically talking about having a reference to the child process  
and  
doing anything with it. It involves “really close to the metal” Win32  
API  
calls.

For an example, have a look at the implementation of  
InstikiController.startin the following file:  
[http://dev.instiki.org/browser/instiki/trunk/test/watir/e2e.rb](http://dev.instiki.org/browser/instiki/trunk/test/watir/e2e.rb)

I probably don’t understand the second half of your question, because I  
read  
it as “how do I dynamically construct strings from variables”. This is  
covered in the first 100 pages of every language tutorial, so nobody  
should  
ever have to ask this question on the list.

Alex

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/4 "2007-03-16T22:25:32Z")

</div>

Jan F. wrote:

> How about this:
> 
> # – BEGIN OF EXAMPLE
> 
> # setting command, param, input, output
> 
> # …
> 
> cmd = \<\<END\_OF\_STRING  
> echo off  
> cd path\executabledir  
> #{command} -S #{param} #{input} #{output}  
> END\_OF\_STRING
> 
> system(cmd)
> 
> # – END OF EXAMPLE

That makes perfect sense, but for some reason I’m getting this error:  
: can’t find string “END\_OF\_STRING” anywhere before EOF  
: syntax error, unexpected $end, expecting tSTRING\_CONTENT or  
tSTRING\_DBEG or tSTRING\_DVAR or tSTRING\_END

I’m still working on it, but this solution seems to be ideal as long as  
I can get it to function properly.

Alex, you’re exactly right, I certainly have no problems using variables  
to construct a String. 🙂 I was merely saying that the batch file  
solution was insufficient because I can’t create its content  
dynamically. Thanks for your reply, I’ll check out the file you  
provided.

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/5 "2007-03-16T22:35:47Z")

</div>

On 16.03.2007 21:58, Jan F. wrote:

> system(cmd)
> 
> # – END OF EXAMPLE

You can even combine that:

system \<\<CMD  
echo off  
cd path  
#{command} -S #{arg}  
CMD

And if you need a longer running shell that executes multiple commands  
you can use popen:

# IO.popen(“bash”, “r+”) do |io|

IO.popen(“cmd”, “r+”) do |io|  
th = Thread.new(io) do |chan|  
chan.each {|line| puts line}  
end

# io.puts “ls -l”

io.puts “dir”  
io.puts “exit”  
io.close\_write

th.join  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/6 "2007-03-16T23:37:40Z")

</div>

Stefano C. wrote:

> Alle venerdÃ¬ 16 marzo 2007, Kevin S. ha scritto:  
> Are you sure that the second END\_OF\_STRING is right at the beginning of  
> the  
> line?
> 
> …
> 
> If you really want some spaces before the closing END\_OF\_STRING, you  
> should  
> use the \<\<- form

Well, I didn’t have any indenting going on, but the \<\<- syntax fixed the  
problem anyway. It’s probably just my crappy editor.

It would seem that everything is working - the system method executes  
and returns true, and if I print out the command, it’s identical to what  
was in my batch file, but the batch file creates the desired output, and  
the system method creates nothing.

Could it be an issue of each line hitting the shell before the previous  
line has completed? I need to research Robert’s IO.popen solution to  
get a better grasp of what’s happening there - perhaps that method is  
more appropriate.

Thanks, everybody, for all your help.

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 16, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/7 "2007-03-16T22:38:50Z")

</div>

Alle venerdÃ¬ 16 marzo 2007, Kevin S. ha scritto:

> That makes perfect sense, but for some reason I’m getting this error:  
> : can’t find string “END\_OF\_STRING” anywhere before EOF  
> : syntax error, unexpected $end, expecting tSTRING\_CONTENT or
> 
> tSTRING\_DBEG or tSTRING\_DVAR or tSTRING\_END

Are you sure that the second END\_OF\_STRING is right at the beginning of  
the  
line? What I mean is that the following code

cmd = \<\<END\_OF\_STRING  
text  
END\_OF\_STRING

will produce exactly the error message you get, because the closing  
END\_OF\_STRING is not at the beginning of the line (there are some  
whitespaces  
before it). The correct form is:

cmd = \<\<END\_OF\_STRING  
text  
END\_OF\_STRING

If you really want some spaces before the closing END\_OF\_STRING, you  
should  
use the \<\<- form:

cmd = \<\<-END\_OF\_STRING  
text  
END\_OF\_STRING

which allows spaces before the closing tag.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![kschell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a698b9/32.png) [@kschell](https://www.ruby-forum.com/u/kschell)\
**Post date:** [March 17, 2007, 10:11am UTC](https://www.ruby-forum.com/t/executing-shell-commands-from-ruby-again/89074/8 "2007-03-17T10:11:46Z")

</div>

On 16.03.2007 23:37, Kevin S. wrote:

> It would seem that everything is working - the system method executes  
> and returns true, and if I print out the command, it’s identical to what  
> was in my batch file, but the batch file creates the desired output, and  
> the system method creates nothing.

The output of system goes to stdout. If you want to capture it the  
popen method is better.

> Could it be an issue of each line hitting the shell before the previous  
> line has completed?

No, normally not. Unless of course you tell the shell to put things  
into background.

> I need to research Robert’s IO.popen solution to  
> get a better grasp of what’s happening there - perhaps that method is  
> more appropriate.

Kind regards

robert
