Skip to content

Simpler way to execute shell commands from within ruby processes

License

Notifications You must be signed in to change notification settings

skorks/subshell_command

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SubshellCommand

If, like me, you always forget when and how (and why) to use backticks or popen or whatever when trying to execute a shell command from within a ruby process, this should hopefully make life a little bit easier.

Installation

Add this line to your application's Gemfile:

gem 'subshell_command'

And then execute:

$ bundle

Or install it yourself as:

$ gem install subshell_command

Usage

Simplest form:

result = SubshellCommand.execute("ls -al")
if result.success?
  puts result.stdout_value
else
  puts result.stderr_value
end

But we can do a little better with a block:

SubshellCommand.execute("ls -al") do |o|
  o.cmd = "pwd"                       # we can override the command
  o.redirect_stderr_to_stdout = true  # we can pipe stderr to stdout
  o.current_directory = "/"           # we can override the working directory
  o.env = {                           # we can provide some extra env vars
    FOO: "bar"
  }    
  o.on_success = ->(r) do             # we can provide a success callback
    puts r.stdout_value
  end
  o.on_failure = ->(r) do             # we can provide a failure callback
    puts r.stderr_value
  end
end

All of the above options have sensible defaults, so you can get away with:

SubshellCommand.execute("ls -al") do |o|
  o.on_success = ->(result) do             
    puts result.stdout_value
  end
end

If all you care about is doing something when the command succeeds.

Also all the code for this is in one file, which is on purpose, so if you'd rather not add an extra gem, just grab the file, dump it into your project and off you go.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/skorks/subshell_command.

License

The gem is available as open source under the terms of the MIT License.

About

Simpler way to execute shell commands from within ruby processes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published