For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
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`
or
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].
The system 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:
require 'open3'
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] and [here]. Examples from our Ruby course material.
Update - 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 articles R115 - Using Ruby with Other Languages [3155] Rake - a build system using code written in Ruby - (2011-02-03) R113 - Ruby - Further Input and Output [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17) [4502] Reading and parsing a JSON object in Ruby - (2015-06-01) [4553] RUby - loading, using, changing, storing JSON format data - (2015-10-23) [4678] Expect with Ruby - a training example to get you started - (2016-05-18)
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
|
4759 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, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
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).
|