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

Discussion in 'Ruby' started by John Smith, Dec 30, 2009.

  1. John Smith

    John Smith Guest

    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/.
     
    John Smith, Dec 30, 2009
    #1
    1. Advertising

  2. John Smith

    fkocherga Guest

    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/
     
    fkocherga, Dec 30, 2009
    #2
    1. Advertising

  3. John Smith

    Gary Wright Guest

    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
     
    Gary Wright, Dec 30, 2009
    #3
  4. John Smith

    John Smith Guest

    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/.
     
    John Smith, Dec 30, 2009
    #4
  5. 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` :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" />

    --
    Phillip Gawlowski
     
    Phillip Gawlowski, Dec 30, 2009
    #5
  6. John Smith

    Gary Wright Guest

    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
     
    Gary Wright, Dec 30, 2009
    #6
  7. John Smith

    John Smith Guest

    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/.
     
    John Smith, Dec 30, 2009
    #7
  8. John Smith

    Gary Wright Guest

    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
     
    Gary Wright, Dec 30, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    972
    loveNUNO
    Nov 20, 2003
  2. Peter A. Schott
    Replies:
    4
    Views:
    603
    Peter A. Schott
    Feb 7, 2006
  3. noydb
    Replies:
    14
    Views:
    1,929
    noydb
    Nov 20, 2010
  4. Robert Cohen
    Replies:
    3
    Views:
    310
    Andrew Durstewitz
    Jul 15, 2003
  5. Li Chen
    Replies:
    0
    Views:
    144
    Li Chen
    Dec 20, 2006
Loading...

Share This Page