Dinesh U. [email protected] writes:
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)?
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:
Put the following two lines into a file called “shell-script.sh”
in the current working directory:
echo “This is the output of my shell script”
Type the following command to make the shell script executable:
chmod +x shell-script.sh
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:
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.
Put the following lines into a file called “ruby-program.rb”
in this same working directory:
Test the ruby program from the command line, as follows:
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:
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”.