在Ruby脚本中运行命令行命令 [英] Running command line commands within Ruby script

查看:888
本文介绍了在Ruby脚本中运行命令行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过Ruby运行命令行命令?我试图创建一个小的Ruby程序,它将拨出并通过命令行程序,如'screen','rcsz'等接收/发送。



如果我可以把所有这一切与Ruby(MySQL后端等)绑定,那将是巨大的。

解决方案

有几种方法:






a。使用%x 或'`':

 %x(echo hi)#=> hi \\\

%x(echo hi>& 2)#=> (prints'hi'to stderr)

`echo hi`#=> hi \\\

`echo hi>& 2`#=> (打印'hi'到stderr)

这些方法将返回stdout,

使用系统 $ c>:

  system'echo hi'#=> true(prints'hi')
system'echo hi>& 2'#=> true(打印'hi'到stderr)
system'exit 1'#=> nil

此方法返回 true 那是成功的。

c。使用将所有输出重定向到程序的



< exec

  fork {exec'sleep 60'}你看到一个新的进程, 睡眠,但没有额外的ruby过程。 
exec'echo hi'#prints'hi'
#代码永远不会在这里。

用命令创建的进程替换当前进程。





d。(ruby 1.9)use spawn

 产生睡眠1; echo one'#=> 430 
spawn'echo two'#=> 431
sleep 2
#此程序将打印two \\\
one。

此方法不等待进程退出并返回PID。






e。使用 IO.popen

  io = IO.popen'cat','r +'
$ stdout = io
puts'hi'
$ stdout = IO.new 0
p io.read(1)
io.close
#prints'h'。

此方法将返回一个 IO 表示新进程的输入/输出。它也是目前我知道给予程序输入的唯一方式。






f。使用 Open3 (在1.9.2及更高版本)

  require'open3'

stdout, stderr,status = Open3.capture3(some_command)
STDERR.puts stderr
if status.successful?
put stdout
else
STDERR.putsOH NO!
end

Open3 其他功能用于获得对两个输出流的显式访问。它类似于popen,但允许您访问stderr。


Is there a way to run command line commands through Ruby? I'm trying to create a small little Ruby program that would dial out and receive/send through command line programs like 'screen', 'rcsz', etc.

It would be great if I could tie all this in with Ruby (MySQL backend, etc.)

解决方案

Yes. There are several ways:


a. Use %x or '`':

%x(echo hi) #=> "hi\n"
%x(echo hi >&2) #=> "" (prints 'hi' to stderr)

`echo hi` #=> "hi\n"
`echo hi >&2` #=> "" (prints 'hi' to stderr)

These methods will return the stdout, and redirect stderr to the program's.


b. Use system:

system 'echo hi' #=> true (prints 'hi')
system 'echo hi >&2' #=> true (prints 'hi' to stderr)
system 'exit 1' #=> nil

This method returns true if the command was successful. It redirects all output to the program's.


c. Use exec:

fork { exec 'sleep 60' } # you see a new process in top, "sleep", but no extra ruby process. 
exec 'echo hi' # prints 'hi'
# the code will never get here.

That replaces the current process with the one created by the command.


d. (ruby 1.9) use spawn:

spawn 'sleep 1; echo one' #=> 430
spawn 'echo two' #=> 431
sleep 2
# This program will print "two\none".

This method does not wait for the process to exit and returns the PID.


e. Use IO.popen:

io = IO.popen 'cat', 'r+'
$stdout = io
puts 'hi'
$stdout = IO.new 0
p io.read(1)
io.close
# prints '"h"'.

This method will return an IO object that reperesents the new processes' input/output. It is also currently the only way I know of to give the program input.


f. Use Open3 (on 1.9.2 and later)

require 'open3'

stdout,stderr,status = Open3.capture3(some_command)
STDERR.puts stderr
if status.successful?
  puts stdout
else
  STDERR.puts "OH NO!"
end

Open3 has several other functions for getting explicit access to the two output streams. It's similar to popen, but gives you access to stderr.

这篇关于在Ruby脚本中运行命令行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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