Forming sanitary shell commands or system calls in Ruby
It doesn't look like you need a shell for what you're doing. See the documentation for system
here: http://ruby-doc.org/core/classes/Kernel.html#M001441
You should use the second form of system
. Your example above would become:
system 'usermod', '-p', @options['shadow'], @options['username']
A nicer (IMO) way to write this is:
system *%W(usermod -p #{@options['shadow']} #{@options['username']})
The arguments this way are passed directly into the execve
call, so you don't have to worry about sneaky shell tricks.
If you need not just the exit status but also the result you probably want to use Open3.popen3
:
require 'open3'
stdin, stdout, stderr = Open3.popen3('usermod', '-p', @options['shadow'], @options['username'])
stdout.gets
sterr.gets
More information here: Getting output of system() calls in Ruby