Skip to content

Instantly share code, notes, and snippets.

@StasKoval
Last active August 29, 2015 14:09
Show Gist options
  • Save StasKoval/fa2c2d038b7217cb6056 to your computer and use it in GitHub Desktop.
Save StasKoval/fa2c2d038b7217cb6056 to your computer and use it in GitHub Desktop.
# Run external command and abort on failure
def shell(*cmd)
return if cmd.empty?
prog = cmd.first.to_s.split.first
abort "#{prog}: command not found" if `which #{prog}`.empty?
system *cmd
abort "#{prog}: interrupted, signal #{$?.termsig}" if $?.signaled?
abort "#{prog}: execution failed, exit code #{$?.exitstatus}" unless $?.success?
end
# Example of task using method shell()
task :test_shell do
# print something
shell "echo run command from a string"
shell *%W{echo run command from an array, and preserve #{'s p a c e s'}}
# can be interrupted with Ctrl-C
shell 'sleep 10'
# should fail with code 51
shell *%W{sh -c #{"exit 51"}}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment