hiding system command results in ruby - 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.

To keep it working with system without modifying your command:
system('curl ...', :err => File::NULL)
Source

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.

Easiest ways other than popen:
Use %x instead of system. It will automatically pipe
rval = %x{curl ...} #rval will contain the output instead of function return value
Manually pipe to /dev/null. Working in UNIX like system, not Windows
system "curl ... > /dev/null"

The simplest one is to redirect stdout :)
system "curl ... 1>/dev/null"
# same as
`curl ... 1>/dev/null`

Related

How to ignore output for a parameterized Ruby system call

I found some code that allows me to run an Applescript from Ruby via system calls:
system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten
It works great, but it's not exactly what I need.
I want to modify the system call so that standard output is ignored.
I started by adding 1>/dev/null parameter:
system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten, "1>/dev/null"
This didn't work as the third parameter appears to be ignored.
Then I removed the parameterized call and used string interpolation:
system "osascript #{*script.split(/\n/).map { |line| ['-e', line] }.flatten} 1>/dev/null"
which produced a syntax error.
Then I tried various attempts at moving things around, which mostly produced other syntax errors.
What's the correct syntax for ignoring system output in this case?
This would be easier if you switched from Kernel#system to Open3. The methods in Open3 give you convenient control over stdin, stdout, and stderr without having to mess around, you could even feed your script into osascript through stdin rather than splitting it apart and using multiple -e switches.
Something like this perhaps:
out, error, status = Open3.capture3('osascript', stdin_data: script)
And if you want to ignore stdout and stderr, use placeholder variables:
_, _, status = Open3.capture3('osascript', stdin_data: script)
*_, status = Open3.capture3('osascript', stdin_data: script)
And if you don't care about stdout, stderr, or the status:
Open3.capture3('osascript', stdin_data: script)
But your conscience should tell you to at least check the status.
The available options for system, exec and spawn are documented in Kernel#spawn, including those for redirection. To redirect stdout to /dev/null you'd use:
system('...', out: '/dev/null')
Or via File::NULL:
system('...', out: File::NULL)

Is it possible to capture output from a system command and redirect it?

What I would like to do is:
run a ruby script...
that executes a shell command
and redirects it to a named pipe accessible outside the script
from the system shell, read from that pipe
That is, have the Ruby script capture some command output and redirect it in such a way that it's connectable to from outside the script?
I want to mention that the script cannot simply start and exit, since it's a REPL. The idea is that using the REPL you would be able to run a command and redirect its output elsewhere to consume it.
Using abort and an exit message, will pass the message to STDERR (and the script will fail with exit code 1). You can pass this shell command output in this way.
This is possibly not the only (or best) way, but it has worked for me in the past.
[edit]
You can also redirect the output to a file (using standard methods), and read that file outside the ruby script.
require 'open3'
stdin, stderr, status = Open3.capture3(commandline)
stdin.chomp #Here, you should ge
Incase, if someone wanted to use you can get the output via stdin.chomp

bash assign output of fuser to a variable oddity

when using the following bash script to assign variable to the output of fuser, it still outputs the part of result(before :) of fuser to screen.
why isn't it suppressed? I suspect it has to do with the ":" char output by fuser. how do I fix this?
test=`fuser -f /home/whois_database_collection_v4/whoisdatacollector/logs/com_log_2013_02_15_12_40_43.log`
/home/whois_database_collection_v4/whoisdatacollector/logs/com_log_2013_02_15_12_40_43.log:
fuser sends the part of the output you see to stderr, and the rest to stdout (I suspect that it tries to simplify its usage from scripts, while preserving some beauty from the user's point of view). It will disappear if you add 2> /dev/null redirection for stderr.

Ruby not showing output of internal process

I'm trying this in ruby.
I have a shell script to which I can pass a command which will be executed by the shell after some initial environment variables have been set. So in ruby code I'm doing this..
# ruby code
my_results = `some_script -allow username -cmd "perform_action"`
The issue is that since the script "some_script" runs "perform_action" in it's own environment, I'm not seeing the result when i output the variable "my_results". So a ruby puts of "my_results" just gives me some initial comments before the script processes the command "perform_action".
Any clues how I can get the output of perform_action into "my_results"?
Thanks.
The backticks will only capture stdout. If you are redirecting stdout, or writing to any other handle (like stderr), it will not show up in its output; otherwise, it should. Whether something goes into stdout or not is not dependent on an environment, only on redirection or direct writing to a different handle.
Try to see whether your script actually prints to stdout from shell:
$ some_script -allow username -cmd "perform_action" > just_stdout.log
$ cat just_stdout.log
In any case, this is not a Ruby question. (Or at least it isn't if I understood you correctly.) You would get the same answer for any language.

clojure -- how to run a program without piping it's output?

I want to use something like shell-out [ http://richhickey.github.com/clojure-contrib/shell-out-api.html ], but without capturing the any output. Of course the output can be passed to print, but this is slightly less than desirable (e.g. in the case that the subprocess may fail).
edit
Sorry, I want the subprocess to output to the same stdout as the parent process.
Also see this third party library
https://github.com/Raynes/conch
It provides direct access to the streams.
EDIT: Before Clarification
You can wrap the shell command with a sh and then pipe to /dev/null like so:
(clojure.java.shell/sh "sh" "-c" "echo hello > /dev/null")
;; {:exit 0, :out "", :err ""}
This will silence the output before getting to clojure.
EDIT: After Clarification
Passing output and stderr to print should work as long as the output comes out quickly enough. If you want something with continuous output of error messages and standard output, looking at the source for the "sh" function should help.
Personally, I would make my own version of clojure.java.shell/sh and for each stream, create a thread that pipes the output directly to out using something like IOUtils.copy from org.apache.commons.io.IOUtilsin

Resources