# Call console command and return as string...problem

**URL:** <https://www.ruby-forum.com/t/call-console-command-and-return-as-string-problem/134163>\
**Category:** Ruby\
**Created:** [April 8, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/call-console-command-and-return-as-string-problem/134163 "2008-04-08T15:14:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 8, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/call-console-command-and-return-as-string-problem/134163/1 "2008-04-08T15:14:49Z")

</div>

Hi,

I am trying to use the following line of code in a rails application,  
but I dont think my problem has anything to do with rails, but with  
ruby, so this is why I post it here. To set the scene - I am programming  
an application for biological sequence analysis (among other things…).

I want to pass the contents of an array (DNA sequences) to an external  
program. This program, however, needs a file as input and doesnt seem to  
take input from STDIN (its written in C so I have no clue how to mess  
with its code to change that…).  
So instead I write the sequences into a file and pass the name to the  
program. So far so good. But if I run it like this

@result = %x{program-executable -o infile.txt}

the variable @result is empty (should be a string, right?). If I call  
the exact same command from the command line, it prints the output to  
the screen, so I figured that it should go into the @result variable,  
no?

Any suggestions on what I am doing wrong? Also, if by chance, anybody  
has an idea on how to pass the variable to the program instead of going  
via the route with a physical file, I’d be glad to hear it.

Cheers,  
Marc

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 8, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/call-console-command-and-return-as-string-problem/134163/2 "2008-04-08T16:08:43Z")

</div>

On Tue, Apr 8, 2008 at 3:14 PM, Marc H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with its code to change that…).  
> So instead I write the sequences into a file and pass the name to the  
> program. So far so good. But if I run it like this
> 
> @result = %x{program-executable -o infile.txt}
> 
> the variable @result is empty (should be a string, right?). If I call  
> the exact same command from the command line, it prints the output to  
> the screen, so I figured that it should go into the @result variable,  
> no?

The command seems fine. Parameter -o seem suspicious since it is  
usually used for output.  
Anyway, I guess this is an exception.

Try redirecting stderr to either file (2\> errr.log) or to stdout  
(2\>&1, works even on windows!) to  
see if there are any errors.

> Any suggestions on what I am doing wrong? Also, if by chance, anybody  
> has an idea on how to pass the variable to the program instead of going  
> via the route with a physical file, I’d be glad to hear it.

See IO.popen and/or Open3.popen3

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 9, 2008, 3:34am UTC](https://www.ruby-forum.com/t/call-console-command-and-return-as-string-problem/134163/3 "2008-04-09T03:34:20Z")

</div>

That kind of depends on the executable you’re running.

if you do @results = %x{ls} what do you get (assuming you’re on a unix)

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
