Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hiding system command results in ruby

How easy is it to hide results from system commands in ruby? For example, some of my scripts run

system "curl ..." 

and I would not prefer to see the results of the download.

like image 206
dejay Avatar asked Jun 06 '12 20:06

dejay


People also ask

What happens when Exec is executed in Ruby?

You can also catch the result. Exec is very limited in functionality and when executed will exit the Ruby program and run the command. The System command runs in a sub-shell instead of replacing the current process and returns true or nill.

How to run an external command in Ruby?

There are a few Ruby methods you can use. Depending on the method you use you’ll get different results. Let’s explore these methods together! The Ruby system method is the simplest way to run an external command. Notice that system will print the command output as it happens. Also system will make your Ruby program wait until the command is done.

What is the system method in Ruby?

The systemmethod calls a system program. You have to provide the command as a string argument to this method. For example: >> system("date") Wed Sep 4 22:03:44 CEST 2013 => true The invoked program will use the current STDIN, STDOUTand STDERRobjects of your Ruby program. In fact, the actual return value is either true, falseor nil.

What is the difference between system command and Exec in Ruby?

The system command can return a true if the command was successful or nill when not. echo "hello world" Will output "hello world" in the command window. You can also catch the result. Exec is very limited in functionality and when executed will exit the Ruby program and run the command.


1 Answers

You can use the more sophisticated popen3 to have control over STDIN, STDOUT and STDERR separately if you like:

Open3.popen3("curl...") do |stdin, stdout, stderr, thread|
  # ...
end

If you want to silence certain streams you can ignore them, or if it's important to redirect or interpret that output, you still have that available.

like image 137
tadman Avatar answered Sep 23 '22 16:09

tadman