From: Kev Jackson Date: 2006-05-03T18:29:08+09:00 Subject: Re: Calling Shell Scripts from Ruby ? >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". > > > 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/