Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to run one script from another, and capture the output

Reply
Thread Tools

How to run one script from another, and capture the output

 
 
John Smith
Guest
Posts: n/a
 
      12-30-2009
I have the following hypothetical script that requires arguments to run.
The command line execution looks like this:

'ruby distance_traveled.rb 60 2.5'

The output would be the following:

'You have traveled a distance of 150 miles.'


How would I run the above execution from inside another script, and
capture the output above (the 150 miles portion at least)?

Thanks in advance!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
fkocherga
Guest
Posts: n/a
 
      12-30-2009
On Dec 29, 2009, at 7:11 PM, John Smith wrote:

> I have the following hypothetical script that requires arguments to =

run.
> The command line execution looks like this:
>=20
> 'ruby distance_traveled.rb 60 2.5'
>=20
> The output would be the following:
>=20
> 'You have traveled a distance of 150 miles.'
>=20
>=20
> How would I run the above execution from inside another script, and
> capture the output above (the 150 miles portion at least)?
>=20
> Thanks in advance!
> --=20
> Posted via http://www.ruby-forum.com/.
>=20



I've faced an exactly same problem when was testing command line =
utilities, by capturing an output and comparing it with expected one. =
Here is a gem for it:

http://github.com/fkocherga/cmd_line_test=20

You may look on running and capturing code there.=20

--
Fedor Kocherga
http://sidenotes.kocherga.info/







 
Reply With Quote
 
 
 
 
Gary Wright
Guest
Posts: n/a
 
      12-30-2009

On Dec 29, 2009, at 8:11 PM, John Smith wrote:

> I have the following hypothetical script that requires arguments to =

run.
> The command line execution looks like this:
>=20
> 'ruby distance_traveled.rb 60 2.5'
>=20
> The output would be the following:
>=20
> 'You have traveled a distance of 150 miles.'
>=20
>=20
> How would I run the above execution from inside another script, and
> capture the output above (the 150 miles portion at least)?



output =3D `ruby distance_traveled.rb 60 2.5`

The backticks cause the standard output of the command to be captured =
and stored in a Ruby string.


Gary Wright




 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      12-30-2009
Gary Wright wrote:
> On Dec 29, 2009, at 8:11 PM, John Smith wrote:
>
>> How would I run the above execution from inside another script, and
>> capture the output above (the 150 miles portion at least)?

>
>
> output = `ruby distance_traveled.rb 60 2.5`
>
> The backticks cause the standard output of the command to be captured
> and stored in a Ruby string.
>
>
> Gary Wright


Thanks so much for that!

One more quick question. If distance_traveled.rb sits in a different
directory than the current script that's calling it, how would I specify
it? For example, say it is in ~project/lib/distance_traveled.rb

Thanks again!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Phillip Gawlowski
Guest
Posts: n/a
 
      12-30-2009
On 30.12.2009 14:06, John Smith wrote:

> One more quick question. If distance_traveled.rb sits in a different
> directory than the current script that's calling it, how would I specify
> it? For example, say it is in ~project/lib/distance_traveled.rb


output = `ruby ~project/lib/distance_traveled.rb 60 2.5`

Or perform string interpolation, if you don't want to hardcode anything:
path = "~/project/lib/"
script = "script.rb"
args = "my args"

output = `#{path}#{script} #{args}`

<warning type="security implications in accepting user input" />

--
Phillip Gawlowski

 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      12-30-2009

On Dec 30, 2009, at 8:06 AM, John Smith wrote:
> One more quick question. If distance_traveled.rb sits in a different=20=


> directory than the current script that's calling it, how would I =

specify=20
> it? For example, say it is in ~project/lib/distance_traveled.rb


The command is parsed by the shell so ~project/lib/distance_traveled.rb =
should work or a relative or absolute path of course.

Gary Wright




 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      12-30-2009
Gary Wright wrote:
> On Dec 30, 2009, at 8:06 AM, John Smith wrote:
>> One more quick question. If distance_traveled.rb sits in a different
>> directory than the current script that's calling it, how would I specify
>> it? For example, say it is in ~project/lib/distance_traveled.rb

>
> The command is parsed by the shell so ~project/lib/distance_traveled.rb
> should work or a relative or absolute path of course.
>
> Gary Wright


Thanks for the help!

However, I should also add that in the above example,
distance_traveled.rb requires other .rb files sitting in the same lib
directory.

I have no problem running distance_traveled.rb from inside lib. However,
from another directory, when I try to run the program using relative
path (ie. 'ruby ../../lib/distance_traveled.rb'), I get
"distance_traveled.rb:1:in `require': no such file to load -- ", in
reference to the other .rb file(s) in the lib directory.

Any suggestions for this issue? Thanks again!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      12-30-2009

On Dec 30, 2009, at 12:24 PM, John Smith wrote:
>=20
> However, I should also add that in the above example,=20
> distance_traveled.rb requires other .rb files sitting in the same lib=20=


> directory.
>=20
> I have no problem running distance_traveled.rb from inside lib. =

However,=20
> from another directory, when I try to run the program using relative=20=


> path (ie. 'ruby ../../lib/distance_traveled.rb'), I get=20
> "distance_traveled.rb:1:in `require': no such file to load -- ", in=20
> reference to the other .rb file(s) in the lib directory.
>=20
> Any suggestions for this issue? Thanks again!


If the files aren't in standard library locations, you'll want to make =
sure that you name them relative to the source file using something =
like:

require File.join(File.expand_path(File.dirname(__FILE__)) , 'otherfile')

This ensures that the require method sees a fully qualified path to =
'otherfile' where 'otherfile' is found relative to the directory =
containing the source file instead of relative to the current working =
directory of the process.


Gary Wright




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to run an EXE, with argument, capture output value noydb Python 14 11-20-2010 02:39 AM
Best way to capture output from an exec'ed (or such) script? exscape@gmail.com Python 6 08-05-2007 04:56 PM
run perl script and capture the output Li Chen Ruby 0 12-20-2006 04:25 AM
Kindly help :::How can I send the output of the system() command to a file and capture a string in one of the lines in that file prashant_ec2003@yahoo.com Perl Misc 8 06-21-2005 04:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57