如何从 Ruby 调用 shell 命令 [英] How to call shell commands from Ruby

查看:19
本文介绍了如何从 Ruby 调用 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Ruby 程序内部调用 shell 命令?我如何将这些命令的输出返回到 Ruby 中?

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?

推荐答案

此解释基于注释 Ruby 脚本 来自我的一个朋友.如果您想改进脚本,请随时通过链接进行更新.

This explanation is based on a commented Ruby script from a friend of mine. If you want to improve the script, feel free to update it at the link.

首先,请注意,当 Ruby 调用 shell 时,它通常调用 /bin/sh而不是 Bash.并非所有系统上的 /bin/sh 都支持某些 Bash 语法.

First, note that when Ruby calls out to a shell, it typically calls /bin/sh, not Bash. Some Bash syntax is not supported by /bin/sh on all systems.

以下是执行 shell 脚本的方法:

Here are ways to execute a shell script:

cmd = "echo 'hi'" # Sample string that can be used

  1. Kernel#` ,通常称为反引号 - `cmd`

  1. Kernel#` , commonly called backticks – `cmd`

这与许多其他语言一样,包括 Bash、PHP 和 Perl.

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

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

Returns the result (i.e. standard output) of the shell command.

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

value = `echo 'hi'`
value = `#{cmd}`

  • 内置语法,%x( cmd )

    x 字符后面是一个定界符,可以是任何字符.如果分隔符是字符 (, [, {, or <,文字由匹配结束分隔符之前的字符组成,考虑到嵌套的分隔符对.对于所有其他分隔符,文字包括直到下一次出现的字符分隔符.字符串插值 #{ ... } 是允许的.

    Following the x character is a delimiter, which can be any character. 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.

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

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

    文档:https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-Percent+Strings

    value = %x( echo 'hi' )
    value = %x[ #{cmd} ]
    

  • 内核#system

    在子shell中执行给定的命令.

    Executes the given command in a subshell.

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

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

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

    wasGood = system( "echo 'hi'" )
    wasGood = system( cmd )
    

  • Kernel#exec

    通过运行给定的外部命令替换当前进程.

    Replaces the current process by running the given external command.

    不返回,当前进程被替换,不再继续.

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

    文档: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
    

  • 这里有一些额外的建议:$?,与$CHILD_STATUS相同,如果使用反引号,则访问上次系统执行命令的状态,system()%x{}.然后您可以访问 exitstatuspid 属性:

    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{}. You can then access the exitstatus and pid properties:

    $?.exitstatus
    

    更多阅读:

    这篇关于如何从 Ruby 调用 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆