Quick and Simple Executor of Bash Commands

DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version Test Coverage Yard Docs Hits-of-Code License

First, install it:

gem install qbash

Simply execute a bash command from Ruby:

require 'qbash'
stdout = qbash('echo "Hello, world!"')

If the command fails, an exception will be raised.

It's possible to provide the standard input and environment variables:

stdout = qbash('cat > $FILE', env: { 'FILE' => 'a.txt' }, stdin: 'Hello!')

It's possible to configure the logging facility too, with the help of the loog gem.

You can also make it return both stdout and exit code, with the help of the both option set to true:

stdout, code = qbash('cat a.txt', both: true, accept: [])

Here, the accept param contains the list of exit codes that are "acceptable" and won't lead to runtime failures. When the list is empty, all exists are acceptable (there will be no failures ever).

The command may be provided as an array, which automatically will be converted to a string by joining all items with spaces between them.

It is very much recommended to escape all command-line values with the help of Shellwords.escape.

How to contribute

Read these guidelines. Make sure you build is green before you contribute your pull request. You will need to have Ruby 3.0+ and Bundler installed. Then:

bundle update
bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.