How can an external command return both its output and its exit status to Ruby?

This is a follow up question regarding ruby system command check exit code. I want to run command such that to get its output as well as exit code. Currently what I used in the code is: rv = `#{cmd} 2>&1` But this only captures output, and rv = system(cmd) only captures the exit code. How to achieve both? Check $?.exitstatus for the exit code. For more info, see http://www.ruby-

外部命令如何将其输出和退出状态返回给Ruby?

这是关于ruby系统命令检查退出代码的后续问题。 我想运行命令以获取其输出以及退出代码。 目前我在代码中使用的是: rv = `#{cmd} 2>&1` 但这只能捕捉输出,而且 rv = system(cmd) 仅捕获退出代码。 如何实现两者? 检查退出代码的$?.exitstatus 。 欲了解更多信息,请参阅http://www.ruby-doc.org/core-2.1.0/Process/Status.html 反引号将捕获命令的输出。 例如,要将输出存储在rv变量中: rv = `echo Er

Run linux command in ruby

I'm trying to do the following.. if "ps | grep -e file" then true; else false; What would I need to do to make the string n the if statement execute the linux command listed? Simply use system("command") . It'll return true if the command was successfully executed. EDIT Just read your question again, and I believe that what you're looking for is this

在ruby中运行linux命令

我正在尝试执行以下操作.. if "ps | grep -e file" then true; else false; 我需要做什么来使字符串n的if语句执行列出的linux命令? 只需使用system("command") 。 如果命令成功执行,它将返回true。 编辑只是再次阅读你的问题,我相信你要找的是这样的: if `ps | grep -e file`.empty? # no matches true else false end 这已经在这里被问到了:从Ruby调用shell命令 简单的答案是

When to use each method of launching a subprocess in Ruby

1. `` The Backtick defined in Kernel 1. a) %x{} Percent X < alternate syntax for The Backtick defined in parse.y, see discussion 2. system() Kernel#system 3. fork() Kernel#fork, Process#fork 4. open() open a pipe Kernel#open 4.a. IO.popen() < behaves the same as open() open a pipe IO#popen 4.b. open("|-") fork to a pipe 4.c. IO.popen("-"

何时使用在Ruby中启动子进程的每种方法

1. `` The Backtick” 在Kernel中定义 1. a) %x{} Percent X <The Backtick的替代语法 在parse.y中定义,参见讨论 2. system() 内核#系统 3. fork() 内核#fork,进程#fork 4. open() 打开一个管道 内核#开 4.A. IO.popen() <表现方式与open()相同 打开一个管道 IO#POPEN 4.B. open("|-") 叉到管道 4.C. IO.popen("-") <与open("|-")行为相同

Use terminal command in ruby code?

This question already has an answer here: Calling shell commands from Ruby 19 answers Kernel#exec , that replaces your ruby process with the one you specified, as a corresponding syscall. Therefore, it ends the program even if there's more code to run. Probably not what you want. Works like: exec("this") Backticks. `this` will run this and return its stdout as a string. T

在ruby代码中使用终端命令?

这个问题在这里已经有了答案: 从Ruby 19调用shell命令的答案 Kernel#exec ,用你指定的那个替换你的ruby进程,作为相应的系统调用。 因此,即使有更多的代码要运行,它也会结束程序。 可能不是你想要的。 像下面这样工作: exec("this") 反引号。 `this`将会运行this `this`并将它的stdout作为一个字符串返回。 同样的东西有不同的语法: %x(this) Kernel#system :与exec相同,但不会替换您的Ruby进程

Execute bash commands from a Rakefile

This question already has an answer here: Calling shell commands from Ruby 19 answers I think the way rake wants this to happen is with: http://rubydoc.info/gems/rake/FileUtils#sh-instance_method Example: task :test do sh "ls" end The built-in rake function sh takes care of the return value of the command (the task fails if the command has a return value other than 0) and in addition it a

从Rakefile执行bash命令

这个问题在这里已经有了答案: 从Ruby 19调用shell命令的答案 我认为耙子想要发生的方式是:http://rubydoc.info/gems/rake/FileUtils#sh-instance_method例如: task :test do sh "ls" end 内置rake函数sh负责处理命令的返回值(如果命令的返回值不是0,则任务失败),并且还会输出命令输出。 有几种方法可以在ruby中执行shell命令。 一个简单的(也许是最常见的)是使用反引号: task :hello do `echo "World!"`

Check if a private key is available from an agent using Ruby's Net::SSH

I'm using Net::SSH to automate access to remove hosts from a ruby program, using key authentication. The program does not dictate where the user should put the private key, instead relying on the user's SSH agent to provide the required keys (as it should). The problem is if the required private key is not available, the connection will fail - and this may happen a long way into the pr

使用Ruby的Net :: SSH检查代理是否有私钥可用

我使用Net :: SSH来自动化访问,使用密钥认证从ruby程序中删除主机。 该程序并没有规定用户应该把私钥放在哪里,而是依靠用户的SSH代理来提供所需的密钥(它应该)。 问题是如果所需的私钥不可用,连接将失败 - 这可能会在程序中发生很长的一段路(SSH连接是我们做了很多其他事情之后做的最后一件事情之一 - 不容易可逆 - 操作)。 假设我知道用户应该拥有什么私钥(如密钥指纹所指定的那样),那么如何从ruby执行此检查 -

how to dowload a file with PubkeyAuthentication=no option

I tried to perform an scp by Ruby with this solution: How do I transfer files using SSH and SCP using Ruby calls? But when I execute, it prints: Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text. Enter passphrase for /home/user/.ssh/id_dsa: But the host is a key-less server, I want to remove this "Enter passphrase for /home/user/.s

如何使用PubkeyAuthentication = no选项下载文件

我试着用这个解决方案通过Ruby来执行scp:如何使用Ruby调用使用SSH和SCP传输文件? 但是当我执行时,它会打印: Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text. Enter passphrase for /home/user/.ssh/id_dsa: 但主机是一个无键服务器,我想删除这个“输入/home/user/.ssh/id_dsa:”问题的密码。 如果我在Linux命令行中执行操作: ssh -o PubkeyAuthentica

How to rescue an input in class initialization

I've got a Ruby class and in the initialization of the class, the user provides a filepath and filename, and the class opens the file. def Files def initialize filename @file = File.open(filename, "r").read.downcase.gsub(/[^a-zs]/,"").split end def get_file return @file end end However, the problem is that the user can provide a file that doesn't exist, and if tha

如何在类初始化中拯救输入

我有一个Ruby类,并且在类的初始化过程中,用户提供了一个文件路径和文件名,并且该类将打开该文件。 def Files def initialize filename @file = File.open(filename, "r").read.downcase.gsub(/[^a-zs]/,"").split end def get_file return @file end end 但问题是用户可以提供一个不存在的文件,如果是这种情况,我需要进行救援,以便我们不会向用户显示难看的回应。 我在想的是这样的 def Files

Rubygems. A newly installed gem isn't added to $LOAD

I installed the new stable Ruby release and when I began to install gems I found out that the paths to them isn't added to the Ruby load path after successful installation of the gems. What is the reason of this issue? How can I achieve it? Thanks. Here's my environment: $ lsb_release -d Description: Debian GNU/Linux 5.0.6 (lenny) $ cat ~/.gemrc gem: --no-ri --no-rdoc gemho

RubyGems的。 新安装的gem不会添加到$ LOAD

我安装了新的稳定的Ruby版本,当我开始安装gems时,我发现在成功安装gem后,它们的路径不会添加到Ruby加载路径中。 这个问题的原因是什么? 我怎样才能实现它? 谢谢。 这是我的环境: $ lsb_release -d Description: Debian GNU/Linux 5.0.6 (lenny) $ cat ~/.gemrc gem: --no-ri --no-rdoc gemhome: /home/<username>/.gem gempath: - /home/<username>/.gem $ gem environment RubyGems En

How can i execute 2 or more commands in the same ssh session?

I have the following script: #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' Net::SSH.start('host1', 'root', :password => "mypassword1") do |ssh| stdout = "" ssh.exec("cd /var/example/engines/") ssh.exec!( "pwd" ) do |channel, stream, data| stdout << data if stream == :stdout end puts stdout ssh.loop end and i get /root , instead of /var/exam

我如何在同一个ssh会话中执行2个或更多的命令?

我有以下脚本: #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' Net::SSH.start('host1', 'root', :password => "mypassword1") do |ssh| stdout = "" ssh.exec("cd /var/example/engines/") ssh.exec!( "pwd" ) do |channel, stream, data| stdout << data if stream == :stdout end puts stdout ssh.loop end 我得到/root ,而不是/var/example/engines/ 看看是否