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

J

John Smith

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!
 
F

fkocherga

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
 
G

Gary Wright

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
 
J

John Smith

Gary said:
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!
 
P

Phillip Gawlowski

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` :p

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" />
 
G

Gary Wright

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
 
J

John Smith

Gary said:
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!
 
G

Gary Wright

=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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top