Calling Shell Scripts from Ruby?

Hi all,

I have two questions

1)Is it possible to call “Shell Scripts” from ruby? Say if i have a
shell sciprt named “test.sh”, how can i call this from ruby? Also if i
can call like that,is it possible to get the output of that script
passed back to ruby?

2)How can i call any “Unix command” residing from a ruby program??

Thank You
Dinesh

1)Is it possible to call “Shell Scripts” from ruby? Say if i have a
shell sciprt named “test.sh”, how can i call this from ruby? Also if i
can call like that,is it possible to get the output of that script
passed back to ruby?

2)How can i call any “Unix command” residing from a ruby program??
1 and 2. I always use
output = Unix command

You can also use Kernel.system, but I don’t think that will return your
output
http://www.ruby-doc.org/core/classes/Kernel.html#M002992

Hi Edwin,

Thanks for your reply. When you say output = Unix command, i did not
get about how exactly to use it, is it possible to give a simple sample
examples if you have with you which calls a script ??

Oh…so u mean from shell script we can’t get the return values to ruby.
My requirement is that from “Rails” through the web i will call some
shell script and finally the script should return me a “String” value.
is that not possible from ruby?

Thank You
Dinesh

Edwin van Leeuwen wrote:

1)Is it possible to call “Shell Scripts” from ruby? Say if i have a
shell sciprt named “test.sh”, how can i call this from ruby? Also if i
can call like that,is it possible to get the output of that script
passed back to ruby?

2)How can i call any “Unix command” residing from a ruby program??
1 and 2. I always use
output = Unix command

You can also use Kernel.system, but I don’t think that will return your
output
module Kernel - RDoc Documentation

Thanks for your reply. When you say output = Unix command, i did not
get about how exactly to use it, is it possible to give a simple sample
examples if you have with you which calls a script ??

  1. Start irb. Then do this:
  2. cmd_result = ls

=> "acoc-0.7.1.tar.bz2\n (and much more but i edited it out)

  1. puts cmd_result.class # => String

  2. Create a shell file such as test.sh, with content like
    echo “This is a test”

  3. sh test.sh

Do you see the output?
I see it on my system.

Hope that helped, cheers, shev.

Hi Shev,

Thank for the reply. But my requirement is that i have to call the shell
scripts or Unix Commands from Ruby itself, rather than from IRB ??
Because i will be basicall invoking the ruby from Web (Rails)?

Thank You…

Guest wrote:

Thanks for your reply. When you say output = Unix command, i did not
get about how exactly to use it, is it possible to give a simple sample
examples if you have with you which calls a script ??

  1. Start irb. Then do this:
  2. cmd_result = ls

=> "acoc-0.7.1.tar.bz2\n (and much more but i edited it out)

  1. puts cmd_result.class # => String

  2. Create a shell file such as test.sh, with content like
    echo “This is a test”

  3. sh test.sh

Do you see the output?
I see it on my system.

Hope that helped, cheers, shev.

Dinesh U. wrote:

Just a heads up that if you only follow the ruby-forum you have missed
some excellent replies. See this link for all the replies:
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/191006?190922-191129

Edwin

Dinesh U. wrote:

Hi Shev,

Thank for the reply. But my requirement is that i have to call the shell
scripts or Unix Commands from Ruby itself, rather than from IRB ??
Because i will be basicall invoking the ruby from Web (Rails)?

Thank You…

  1. Start irb. Then do this:
  2. cmd_result = ls

=> "acoc-0.7.1.tar.bz2\n (and much more but i edited it out)

  1. puts cmd_result.class # => String

  2. Create a shell file such as test.sh, with content like
    echo “This is a test”

  3. sh test.sh

Thx shev for being more verbose, that was what I was hinting at. At
heart invoking sh test.sh is the same as invoking any (unix)command and
you can do that by putting the command between ``

As you can see from irb this will also give you the output of the file.

There is no difference between doing something in irb and in a normal
ruby file, except that irb gives you immediate feedback for testing
purposes. So you can just put a_unix_command in your ruby file to
execute commands. (Where you replace a_unix_command by the actual
command/script you want to execute.)

Edwin

Hi Edwin/Shev,

Thank you for all your replies. I will try it out today using the `` in
the AIX box…

BTW Edwin i was not able to get into the below link.
(http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/191006?190922-191129)

But when i used google groups, i saw the reply about the details steps
mentioned there, i guess u are refering to the same here rt?

BTW why does all the replies do NOT come to the ruby-forums ?? But i’m
able to see the replies using the google groups rather (If i search the
words specified in the forum in google groups) ???

Do i need to be a member of google groups if we want to receive all the
replies??

Thank You,
Dinesh

Edwin van Leeuwen wrote:

Dinesh U. wrote:

Just a heads up that if you only follow the ruby-forum you have missed
some excellent replies. See this link for all the replies:
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/191006?190922-191129

Edwin

Guest wrote:

Hi Edwin/Shev,

Thank you for all your replies. I will try it out today using the `` in
the AIX box…

BTW Edwin i was not able to get into the below link.
(http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/191006?190922-191129)

But when i used google groups, i saw the reply about the details steps
mentioned there, i guess u are refering to the same here rt?

BTW why does all the replies do NOT come to the ruby-forums ?? But i’m
able to see the replies using the google groups rather (If i search the
words specified in the forum in google groups) ???

Do i need to be a member of google groups if we want to receive all the
replies??

Thank You,
Dinesh

Edwin van Leeuwen wrote:

Dinesh U. wrote:

Just a heads up that if you only follow the ruby-forum you have missed
some excellent replies. See this link for all the replies:
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/191006?190922-191129

Edwin

See:
http://www.ruby-forum.com/topic/64117#new

and

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/191043

Basically: the forum is broken.

-Justin

On 5/2/06, Dinesh U. [email protected] wrote:

Hi all,

I have two questions

1)Is it possible to call “Shell Scripts” from ruby? Say if i have a
shell sciprt named “test.sh”, how can i call this from ruby?

system( “/path/to/test.sh” )

See: ri Kernel#system

Also if i
can call like that,is it possible to get the output of that script
passed back to ruby?

foo = ./test.sh # Don’t forget to chomp what gets stored in foo.

2)How can i call any “Unix command” residing from a ruby program??

“Unix commands” and your scripts are the same kind of animal:

bar = date

Thank You
Dinesh

Note, if you want your script to be able to chit chat with the
external process it’s running, you’ll want to look at IO.popen.

On May 2, 2006, at 8:22 PM, Dinesh U. wrote:

My requirement is that from “Rails” through the web i will call some
shell script and finally the script should return me a “String”
value.
is that not possible from ruby?

type this in irb: x = ls

Elliot

Dinesh U. wrote:

Thank You
Dinesh

1 & 2) Use test.sh (or system(“test.sh”) if you do NOT want the
output)

-Justin

 puts shell_script_output

from within a ruby program, and capture their output in a variable

This is an example of a post to a mailing list that I wish I could ‘mod
up’ in slashdot speak. A great walkthrough of how to get values froma
shell script into ruby code and an example of how to write a clear
example!

This kind of example should be preserved online (it will automatically
via MARC or Gmane or something), but wouldn’t it be better if there was
something similar to Java Almanac[1] for Ruby?

Kev
[1]http://www.javaalmanac.com/

Dinesh U. [email protected] writes:

Hi Shev,

Thank for the reply. But my requirement is that i have to call the shell
scripts or Unix Commands from Ruby itself, rather than from IRB ??
Because i will be basicall invoking the ruby from Web (Rails)?

Thank You…

I’m sure that Shev assumed that you would test using the irb commands,
and that then you would generalize what you learned from the test and
put that knowledge to use in the non-irb environment. This is the usual
way for us to illustrate the use of ruby code in this forum.

But apparently, his use of irb for illustrative purposes confused you,
so here is a more detailed, step-by-step lesson:

  1. Put the following two lines into a file called “shell-script.sh”
    in the current working directory:

    #!/bin/sh
    echo “This is the output of my shell script”

  2. Type the following command to make the shell script executable:

    chmod +x shell-script.sh

  3. Test the shell script from the command line to make sure that
    it works before proceeding to the code that shows how to
    capture the shell script output in ruby:

    ./shell-script.sh

  4. If you see the words “This is the output of my shell script”,
    then go to step 5; otherwise, something is wrong, and you
    should carefully repeat steps 1 through 3.

  5. Put the following lines into a file called “ruby-program.rb”
    in this same working directory:

    shell_script_output = ./shell-script.sh
    puts shell_script_output

  6. Test the ruby program from the command line, as follows:

    ruby ./ruby-program.rb

  7. It should print out “This is the output of my shell script”. If so,
    then you are done. If it doesn’t print out those words, then
    something is wrong and you should carefully repeat steps 5 and 6.

The code code mentioned in step 5 shows how you can call shell scripts
from within a ruby program, and capture their output in a variable
within that program.

Now that you have completed this exercise, you probably will be able to
understand the following shorter example:

variable_in_your_rails_code = /path/to/other-shell-script.sh

This causes the shell script located at “/path/to/other-shell-script.sh”
to be executed, and its printed results to be stored in the variable
which is called “variable_in_your_rails_code”.