如果我调用一个命令使用内核#系统在Ruby中,我如何得到它的输出?
system("ls")
如果我调用一个命令使用内核#系统在Ruby中,我如何得到它的输出?
system("ls")
当前回答
作为直接系统(…)替代品,您可以使用Open3.popen3(…)
进一步讨论: http://tech.natemurray.com/2007/03/ruby-shell-commands.html
其他回答
虽然使用反勾号或popen通常是您真正想要的,但它实际上并不能回答所提出的问题。可能有捕获系统输出的正当理由(可能是为了自动化测试)。我在谷歌上搜索了一下,找到了一个答案,我想把它贴在这里,以造福他人。
因为我需要这个来测试我的例子使用块设置来捕获标准输出,因为实际的系统调用隐藏在被测试的代码中:
require 'tempfile'
def capture_stdout
stdout = $stdout.dup
Tempfile.open 'stdout-redirect' do |temp|
$stdout.reopen temp.path, 'w+'
yield if block_given?
$stdout.reopen stdout
temp.read
end
end
该方法使用tempfile存储实际数据来捕获给定块中的任何输出。使用示例:
captured_content = capture_stdout do
system 'echo foo'
end
puts captured_content
您可以将系统调用替换为任何内部调用系统的东西。如果需要,还可以使用类似的方法来捕获stderr。
将标准输出捕获到名为val的变量中最简单的解决方案:
val = capture(:stdout) do
system("pwd")
end
puts val
缩短版本:
val = capture(:stdout) { system("ls") }
捕获方法由 active_support / core_ext /内核/ reporting.rb
类似地,我们也可以用:stderr捕获标准错误
我想扩展和澄清一下混沌的答案。
如果你用反勾号包围命令,那么你根本不需要(显式地)调用system()。反勾号执行命令并以字符串形式返回输出。然后你可以像这样把值赋给一个变量:
output = `ls`
p output
or
printf output # escapes newline chars
我发现下面是有用的,如果你需要返回值:
result = %x[ls]
puts result
我特别想列出我的机器上所有Java进程的pid,并使用以下方法:
ids = %x[ps ax | grep java | awk '{ print $1 }' | xargs]
你可以使用一个叫做Frontkick的宝石
Frontkick.exec("echo *")
下面是如何检查和阅读它:
result = Frontkick.exec("echo *")
puts result.successful? #=> true if exit_code is 0
puts result.success? #=> alias to successful?, for compatibility with Process::Status
puts result.stdout #=> stdout output of the command
puts result.stderr #=> stderr output of the command
puts result.exit_code #=> exit_code of the command
puts result.status #=> alias to exit_code
puts result.exitstatus #=> alias to exit_code, for compatibility with Process::Status
puts result.duration #=> the time used to execute the command
Github https://github.com/sonots/frontkick frontkick
创业板页面https://rubygems.org/gems/frontkick