我很好奇,是否有可能在 Chef 之外的 ruby 脚本中使用 shellout?如何设置?
gem install mixlib-shellout
在红宝石脚本中
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!
伊塔:如果你想避免自己创建实例,我通常会在我使用它的脚本中转储这个包装器功能:
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