Running shell (operating system) commands from within Ruby
Ruby is an excellent systems admin / scripting tool, allowing other shells and processes to be run from within a Ruby program in various ways.
Using backtics or the %x notation, commeand output on STDOUT is routed back into a variable - for example:
first = `grep -c option247 ac_20160516 ac_20160517`
second = %x!grep -c option247 ac_20160516 ac_20160517!
and you can embed a whole shell script this way using a here document - see [here]
function allows you to run a command with STDOUT routes to Ruby's STDOUT, and have the job status returned
third = system("grep -c option247 ac_20160516 ac_20160517")
and the exec
syste function transfers control to the job you're running terminating your rubys process!
exec "df -h"
To control STDIN and STDERR as well as STDOUT, you can use shell redirects, for example
fourth = %x!grep -c option247 ac_20160516 ac_20160517 2>&1!
and to run a shell command returning data in parallel while that command is still running, you can use a pipe, for example
handle = IO.popen("grep -c option247 ac_20160516 ac_20160517")
For ultimate flexibility, the open3
module provides yu with control over all three standard input/outputs:
h_in,h_out,h_err = Open3.popen3("grep -c option247 ac_20160516 ac_20160517")
though you need to be very careful of pipe buffering if you're using this.
All the examples above in complete programs (and with sample output) [here]
. Examples from our Ruby course material
- further example [here]
to show setting of environment variables to be passed into shell commands. (written 2016-05-18)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesR113 - Ruby - Further Input and Output 
Expect with Ruby - a training example to get you started - (2016-05-18) 
RUby - loading, using, changing, storing JSON format data - (2015-10-23) 
Reading and parsing a JSON object in Ruby - (2015-06-01) 
Reading and checking user inputs - first lessons - Ruby - (2013-02-17)R115 - Using Ruby with Other Languages 
Rake - a build system using code written in Ruby - (2011-02-03)
Some other Articles
Processing data line by line - iterator in Ruby with yieldReading in XML in Ruby with xmlsimpleSwindon, Chippenham and Melksham - day trips by train to Weymouth start for 2016Running shell (operating system) commands from within RubyExceptions in Ruby - throwing, catching and usingAlternating valuses / flip-flop / toggle - example in RubySeparating detailed data code from the main application - Ruby exampleAnnual review of Melksham Matters for Chamber of Commerce AGMRuby training, half a world away
4749 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).