I am not sure if this makes sense but I am thinking if there is a way to suppress the output shown for a command when run using the system
method in ruby? I mea
If you want to take advantage of the variadic form of Kernel.system, which side-steps the many quoting issues with shells, you can use the same options which Kernel.spawn accepts.
TL;DR - Use :out => File::NULL
to silence output from Kernel.system
Arguments with special characters (spaces, etc.) can cause problems with the shell:
irb(main):001:0> filename_with_spaces = "foo bar.txt"
=> "foo bar.txt"
irb(main):002:0> system "ls -l #{filename_with_spaces}"
ls: bar.txt: No such file or directory
ls: foo: No such file or directory
=> false
So if you are interpolating variables into a system call, it is safer to provide the arguments separately:
irb(main):003:0> system "ls", "-l", filename_with_spaces
-rw-r--r-- 1 nobody nobody 9 Feb 1 16:53 foo bar.txt
=> true
But now we have a problem if we want to hide the output.
irb(main):004:0> system "ls", "-l", filename_with_spaces, "> /dev/null"
ls: > /dev/null: No such file or directory
-rw-r--r-- 1 nobody nobody 9 Feb 1 16:53 foo bar.txt
=> false
We could close STDOUT using the :out => :close
option:
irb(main):005:0> system "ls", "-l", filename_with_spaces, :out => :close
=> true
However, this might cause issues with certain commands which may try to attach to STDOUT.
irb(main):006:0> system "echo", "hi there", :out => :close
echo: write: Bad file descriptor
=> false
To fix this, we can go back to redirecting our output, using File::NULL
to remain portable:
irb(main):007:0> system "echo", "hi there", :out => File::NULL
=> true