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

查看:179
本文介绍了如何从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 not 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字符之后是一个定界符,可以是任何字符. 如果定界符是字符([{<之一, 文字包含直到匹配的结束定界符的字符, 考虑嵌套的定界符对.对于所有其他定界符, 文字包含直到下一次出现的字符 分隔符.允许使用字符串内插#{ ... }.

    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} ]
    

  • Kernel#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
    

  • 这里有一些额外的建议: 如果使用反引号system()%x{},则$?$CHILD_STATUS相同,将访问上次系统执行的命令的状态. 然后,您可以访问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
    

    有关更多阅读,请参见:

    For more reading see:

    • http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands
    • http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html
    • http://tech.natemurray.com/2007/03/ruby-shell-commands.html

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

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