Wednesday, December 14, 2011

Ruby %x, system(), `backtick`, and puppet Facter::Util::Resolution.exec differences

Below are three different ways of running a system command from ruby:
irb(main):014:0> %x('echo "sdfsd"')
sh: echo "sdfsd": command not found
=> ""
irb(main):015:0> system('echo "sdfsd"')
sdfsd
=> true
irb(main):016:0> `echo "sdfsd"`
=> "sdfsd\n"
%x doesn't give you a shell environment, so builtin commands like echo don't exist. system() gives you a shell, but doesn't save the output. Backticks give you a shell environment and return the stdout output.

In puppet if you're using Resolution.exec:
Facter::Util::Resolution.exec('echo "sdfsd"')
Things are not so obvious. Depending on the puppet version, you might get a shell environment and puppet is also doing some checking with 'which' that might result in a 'nil' return if your command is not a simple binary.

0 comments: