Skip to content

Instantly share code, notes, and snippets.

@jjb
Last active January 22, 2016 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjb/906332 to your computer and use it in GitHub Desktop.
Save jjb/906332 to your computer and use it in GitHub Desktop.
runix: The ruby shell command executor you've always wanted.
# Ruby + UNIX = runix
#
# If return status is 0, returns standard out.
# If return status is non-0, raises an exception with standard error in the message.
# (None of the other methods you know about have this behavior)
#
# Great overview of all ruby shell methods:
# http://tech.natemurray.com/2007/03/ruby-shell-commands.html
#
# dependency: open4 gem
require 'open4'
def runix(command)
standard_error_message=nil
out=""
status = Open4::popen4(command) do |p, sin, sout, serr|
standard_error_message=serr.read
out = sout.read
end
# Open4.popen4 will raise an exception in some non-0 return states, but not all
unless 0 == status
raise "The shell command:\n\n#{command}\n\nfailed with status #{status} and this message:\n\n#{standard_error_message}\n\n"
end
out
end
@jjb
Copy link
Author

jjb commented Apr 6, 2011

/cc @ahoward :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment