问题描述
以下Ruby方法之间有什么区别?
What is the difference between the following Ruby methods?
exec
,system
和%x()
或反引号
我知道它们用于通过Ruby以编程方式执行终端命令,但是我想知道为什么有三种不同的方法来执行此操作.
I know they are used to execute terminal commands programmatically via Ruby, but I'd like to know why there are three different ways to do this.
推荐答案
系统
system
方法调用系统程序.您必须将该命令作为此方法的字符串参数提供.例如:
The system
method 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
被调用的程序将使用Ruby程序的当前STDIN
,STDOUT
和STDERR
对象.实际上,实际的返回值是true
,false
或nil
.在示例中,日期是通过STDIN
的IO对象打印的.如果进程以零状态退出,则方法返回true
,如果进程以非零状态退出,则返回false
,如果执行失败,则返回nil
.
The invoked program will use the current STDIN
, STDOUT
and STDERR
objects of your Ruby program. In fact, the actual return value is either true
, false
or nil
. In the example the date was printed through the IO object of STDIN
. The method will return true
if the process exited with a zero status, false
if the process exited with a non-zero status and nil
if the execution failed.
从Ruby 2.6开始,传递exception: true
会引发异常,而不是返回false
或nil
:
As of Ruby 2.6, passing exception: true
will raise an exception instead of returning false
or nil
:
>> system('invalid')
=> nil
>> system('invalid', exception: true)
Traceback (most recent call last):
...
Errno::ENOENT (No such file or directory - invalid)
另一个副作用是全局变量$?
设置为 Process::Status
对象.该对象将包含有关调用本身的信息,包括被调用进程的进程标识符(PID)和退出状态.
Another side effect is that the global variable $?
is set to a Process::Status
object. This object will contain information about the call itself, including the process identifier (PID) of the invoked process and the exit status.
>> system("date")
Wed Sep 4 22:11:02 CEST 2013
=> true
>> $?
=> #<Process::Status: pid 15470 exit 0>
反引号
反引号(``)称系统程序并返回其输出.与第一种方法相反,该命令不是通过字符串提供的,而是将其放在反引号对中.
Backticks (``) call a system program and return its output. As opposed to the first approach, the command is not provided through a string, but by putting it inside a backticks pair.
>> `date`
=> Wed Sep 4 22:22:51 CEST 2013
全局变量$?
也是通过反引号设置的.使用反引号,您还可以使用字符串插值.
The global variable $?
is set through the backticks, too. With backticks you can also make use string interpolation.
%x()
使用%x
是反引号样式的替代方法.它将也返回输出.像它的亲戚%w
和%q
(以及其他)一样,只要括号风格的分隔符匹配,任何分隔符就足够了.这意味着%x(date)
,%x{date}
和%x-date-
都是同义词.像反引号%x
一样可以使用字符串插值.
Using %x
is an alternative to the backticks style. It will return the output, too. Like its relatives %w
and %q
(among others), any delimiter will suffice as long as bracket-style delimiters match. This means %x(date)
, %x{date}
and %x-date-
are all synonyms. Like backticks %x
can make use of string interpolation.
执行
通过使用 Kernel#exec
当前进程(您的Ruby脚本)替换为通过exec
调用的进程.该方法可以将字符串作为参数.在这种情况下,字符串将进行外壳扩展.如果使用多个参数,则第一个参数用于执行程序,而以下参数作为要调用的程序的参数提供.
By using Kernel#exec
the current process (your Ruby script) is replaced with the process invoked through exec
. The method can take a string as argument. In this case the string will be subject to shell expansion. When using more than one argument, then the first one is used to execute a program and the following are provided as arguments to the program to be invoked.
Open3.popen3
有时,所需的信息被写到标准输入或标准错误中,并且您还需要获得对这些信息的控制权.在这里 Open3.popen3
派上用场了:
require 'open3'
Open3.popen3("curl http://example.com") do |stdin, stdout, stderr, thread|
pid = thread.pid
puts stdout.read.chomp
end
这篇关于Ruby,exec,system和%x()或反引号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!