Dinesh Umanath <u_dinesh / yahoo.com> 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".



> 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)
>> 
>> 3) puts cmd_result.class  # => String
>> 
>> 4) Create a shell file such as test.sh, with content like
>> echo "This is a test"
>> 
>> 5) `sh test.sh`
>> 
>> 
>> Do you see the output?
>> I see it on my system.
>> 
>> Hope that helped, cheers, shev.
>
>
> -- 
> Posted via http://www.ruby-forum.com/.
>
>

-- 
 Lloyd Zusman
 ljz / asfast.com
 God bless you.