Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

ruby - 从Ruby调用Shell命令(Calling shell commands from Ruby)

How do I call shell commands from inside of a Ruby program?

(如何从Ruby程序内部调用Shell命令?)

How do I then get output from these commands back into Ruby?

(然后如何将这些命令的输出返回到Ruby?)

  ask by CodingWithoutComments translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This explanation is based on a commented Ruby script from a friend of mine.

(该说明基于我的一个朋友的注释Ruby脚本 。)

If you want to improve the script, feel free to update it at the link.

(如果要改进脚本,请随时在链接上进行更新。)

First, note that when Ruby calls out to a shell, it typically calls /bin/sh , not Bash.

(首先,请注意,当Ruby调用shell时,通常调用/bin/sh 而不是 Bash。)

Some Bash syntax is not supported by /bin/sh on all systems.

(/bin/sh并非在所有系统上都支持某些Bash语法。)

Here are ways to execute a shell script:

(以下是执行Shell脚本的方法:)

cmd = "echo 'hi'" # Sample string that can be used
  1. Kernel#` , commonly called backticks – `cmd`

    (Kernel#` ,通常称为反引号– `cmd`)

    This is like many other languages, including Bash, PHP, and Perl.

    (就像许多其他语言一样,包括Bash,PHP和Perl。)

    Returns the result (ie standard output) of the shell command.

    (返回shell命令的结果(即标准输出)。)

    Docs: http://ruby-doc.org/core/Kernel.html#method-i-60

    (文件: http : //ruby-doc.org/core/Kernel.html#method-i-60)

     value = `echo 'hi'` value = `#{cmd}` 
  2. Built-in syntax, %x( cmd )

    (内置语法%x( cmd ))

    Following the x character is a delimiter, which can be any character.

    (x字符后是分隔符,可以是任何字符。)

    If the delimiter is one of the characters ( , [ , { , or < , the literal consists of the characters up to the matching closing delimiter, taking account of nested delimiter pairs. For all other delimiters, the literal comprises the characters up to the next occurrence of the delimiter character. String interpolation #{ ... } is allowed.

    (如果定界符是字符([{< ,下次出现分隔符时,允许使用字符串插值#{ ... } 。)

    Returns the result (ie standard output) of the shell command, just like the backticks.

    (像反引号一样,返回shell命令的结果(即标准输出)。)

    Docs: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html

    (文件: http : //www.ruby-doc.org/docs/ProgrammingRuby/html/language.html)

     value = %x( echo 'hi' ) value = %x[ #{cmd} ] 
  3. Kernel#system

    Executes the given command in a subshell.

    (在子shell中执行给定命令。)

    Returns true if the command was found and run successfully, false otherwise.

    (如果找到命令并成功运行,则返回true ,否则返回false 。)

    Docs: http://ruby-doc.org/core/Kernel.html#method-i-system

    (文件: http : //ruby-doc.org/core/Kernel.html#method-i-system)

     wasGood = system( "echo 'hi'" ) wasGood = system( cmd ) 
  4. Kernel#exec

    Replaces the current process by running the given external command.

    (通过运行给定的外部命令来替换当前进程。)

    Returns none, the current process is replaced and never continues.

    (不返回任何值,当前进程将被替换且永远不会继续。)

    Docs: http://ruby-doc.org/core/Kernel.html#method-i-exec

    (文件: http : //ruby-doc.org/core/Kernel.html#method-i-exec)

     exec( "echo 'hi'" ) exec( cmd ) # Note: this will never be reached because of the line above 

Here's some extra advice: $?

(这里有一些额外的建议: $?)

, which is the same as $CHILD_STATUS , accesses the status of the last system executed command if you use the backticks, system() or %x{} .

(与$CHILD_STATUS相同,如果使用反引号, system()%x{} ,则访问上次系统执行的命令的状态。)

You can then access the exitstatus and pid properties:

(然后,您可以访问exitstatuspid属性:)

$?.exitstatus

For more reading see:

(有关更多阅读,请参阅:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...