Is it possible to use so = shellout("linux cmd") outside of Chef in ruby script?

2.1k Views Asked by At

I'm curious, is there a possibility to use shellout in ruby scripts outside of Chef? How to set up this?

1

There are 1 best solutions below

4
On

gem install mixlib-shellout

and in the ruby script

require 'mixlib/shellout'
cmd = Mixlib::ShellOut.new('linux cmd')
cmd.run_command
# And then optionally, to raise an exception if the command fails like shell_out!()
cmd.error!

ETA: If you want to avoid creating the instance yourself, I usually dump this wrapper fucntion in scripts where I use it:

def shellout(cmd, ok_exits = [0])
  run = Mixlib::ShellOut.new(cmd)
  run.run_command
  if run.error? || !ok_exits.include?(run.exitstatus)
    puts "#{cmd} failed: #{run.stderr}"
    exit 2
  end
  run.stdout
end