在Ruby脚本中运行命令行命令

时间:2021-06-14 16:24:51

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.

有办法通过Ruby运行命令行命令吗?我正在尝试创建一个小的Ruby程序,它可以通过“screen”、“rcsz”等命令行程序进行拨号和接收/发送。

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

如果我能将所有这些与Ruby (MySQL后端等)结合起来就太好了。

5 个解决方案

#1


186  

Yes. There are several ways:

是的。有几种方法:


a. Use %x or '`':

a.使用%x或' ":

%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.

这些方法将返回stdout,并将stderr重定向到程序的。


b. Use system:

b。使用系统:

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.

如果命令成功,此方法将返回true。它将所有输出重定向到程序的输出。


c. Use exec:

c。使用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:

d. (ruby 1.9)使用衍生工具:

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.

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


e. Use IO.popen:

e。使用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.

该方法将返回一个IO对象,该对象重新存储新进程的输入/输出。这也是目前我所知道的提供程序输入的唯一方法。


f. Use Open3 (on 1.9.2 and later)

f.使用Open3(在1.9.2及以后)

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.

Open3还有其他几个函数用于获取对两个输出流的显式访问。它和popen很相似,但是可以访问stderr。

#2


13  

There's a few ways to run system commands in Ruby.

有几种方法可以在Ruby中运行系统命令。

irb(main):003:0> `date /t` # surround with backticks
=> "Thu 07/01/2010 \n"
irb(main):004:0> system("date /t") # system command (returns true/false)
Thu 07/01/2010
=> true
irb(main):005:0> %x{date /t} # %x{} wrapper
=> "Thu 07/01/2010 \n"

But if you need to actually perform input and output with the command's stdin/stdout, you'll probably want to look at the IO::popen method, which specifically offers that facility.

但是,如果您需要使用命令的stdin/stdout实际执行输入和输出,您可能需要查看IO::popen方法,它专门提供该功能。

#3


6  

 folder = "/"
 list_all_files = "ls -al #{folder}"
 output = `#{list_all_files}`
 puts output

#4


2  

Yes this is certainly doable but the method of implementation differs dependant on whether the "command line" program in question operates in "Full screen" or command line mode. Programs written for the command line tend to read STDIN and write to STDOUT. These can be called directly within Ruby using the standard backticks methods and/or system/exec calls.

是的,这当然是可行的,但是实现方法的不同取决于所讨论的“命令行”程序是在“全屏”运行还是在命令行模式下运行。为命令行编写的程序倾向于读取STDIN并写入STDOUT。可以使用标准回调方法和/或系统/exec调用在Ruby中直接调用这些函数。

If the program operates in "Full Screen" mode like screen or vi then the approach has to be different. For programs like this you should look for a Ruby implementation of the "expect" library. This will allow you to script what you expect to see on screen and what to send when you see those particular strings appear on screen.

如果程序运行在像Screen或vi这样的“全屏”模式下,那么方法必须是不同的。对于这样的程序,您应该寻找“expect”库的Ruby实现。这将允许您编写脚本,以便在屏幕上看到您期望看到的内容,以及当您看到屏幕上出现这些特定字符串时要发送的内容。

This is unlikely to be the best approach and you should probably look at what you are trying to achieve and find the relevant library/gem to do that rather than trying to automate an existing full screen application. As an example "Need assistance with serial port communications in Ruby" deals with Serial Port communications, a pre-cursor to dialing if that is what you want to achieve using the specific programs you mentioned.

这不太可能是最好的方法,您应该看看您想要实现的目标,并找到相关的库/gem来实现这一点,而不是试图自动化一个现有的全屏应用程序。例如,“需要在Ruby中使用串口通信的帮助”处理串口通信,如果您想要使用您提到的特定程序来实现串口通信,则可以使用一个预指针来拨号。

#5


0  

The Most Used method is Using Open3 here is my code edited version of the above code with some corrections:

最常用的方法是使用Open3,这里是我的代码编辑版本,上面的代码有一些修改:

require 'open3'
puts"Enter the command for execution"
some_command=gets
stdout,stderr,status = Open3.capture3(some_command)
STDERR.puts stderr
if status.success?
  puts stdout
else
  STDERR.puts "ERRRR"
end

#1


186  

Yes. There are several ways:

是的。有几种方法:


a. Use %x or '`':

a.使用%x或' ":

%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.

这些方法将返回stdout,并将stderr重定向到程序的。


b. Use system:

b。使用系统:

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.

如果命令成功,此方法将返回true。它将所有输出重定向到程序的输出。


c. Use exec:

c。使用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:

d. (ruby 1.9)使用衍生工具:

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.

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


e. Use IO.popen:

e。使用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.

该方法将返回一个IO对象,该对象重新存储新进程的输入/输出。这也是目前我所知道的提供程序输入的唯一方法。


f. Use Open3 (on 1.9.2 and later)

f.使用Open3(在1.9.2及以后)

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.

Open3还有其他几个函数用于获取对两个输出流的显式访问。它和popen很相似,但是可以访问stderr。

#2


13  

There's a few ways to run system commands in Ruby.

有几种方法可以在Ruby中运行系统命令。

irb(main):003:0> `date /t` # surround with backticks
=> "Thu 07/01/2010 \n"
irb(main):004:0> system("date /t") # system command (returns true/false)
Thu 07/01/2010
=> true
irb(main):005:0> %x{date /t} # %x{} wrapper
=> "Thu 07/01/2010 \n"

But if you need to actually perform input and output with the command's stdin/stdout, you'll probably want to look at the IO::popen method, which specifically offers that facility.

但是,如果您需要使用命令的stdin/stdout实际执行输入和输出,您可能需要查看IO::popen方法,它专门提供该功能。

#3


6  

 folder = "/"
 list_all_files = "ls -al #{folder}"
 output = `#{list_all_files}`
 puts output

#4


2  

Yes this is certainly doable but the method of implementation differs dependant on whether the "command line" program in question operates in "Full screen" or command line mode. Programs written for the command line tend to read STDIN and write to STDOUT. These can be called directly within Ruby using the standard backticks methods and/or system/exec calls.

是的,这当然是可行的,但是实现方法的不同取决于所讨论的“命令行”程序是在“全屏”运行还是在命令行模式下运行。为命令行编写的程序倾向于读取STDIN并写入STDOUT。可以使用标准回调方法和/或系统/exec调用在Ruby中直接调用这些函数。

If the program operates in "Full Screen" mode like screen or vi then the approach has to be different. For programs like this you should look for a Ruby implementation of the "expect" library. This will allow you to script what you expect to see on screen and what to send when you see those particular strings appear on screen.

如果程序运行在像Screen或vi这样的“全屏”模式下,那么方法必须是不同的。对于这样的程序,您应该寻找“expect”库的Ruby实现。这将允许您编写脚本,以便在屏幕上看到您期望看到的内容,以及当您看到屏幕上出现这些特定字符串时要发送的内容。

This is unlikely to be the best approach and you should probably look at what you are trying to achieve and find the relevant library/gem to do that rather than trying to automate an existing full screen application. As an example "Need assistance with serial port communications in Ruby" deals with Serial Port communications, a pre-cursor to dialing if that is what you want to achieve using the specific programs you mentioned.

这不太可能是最好的方法,您应该看看您想要实现的目标,并找到相关的库/gem来实现这一点,而不是试图自动化一个现有的全屏应用程序。例如,“需要在Ruby中使用串口通信的帮助”处理串口通信,如果您想要使用您提到的特定程序来实现串口通信,则可以使用一个预指针来拨号。

#5


0  

The Most Used method is Using Open3 here is my code edited version of the above code with some corrections:

最常用的方法是使用Open3,这里是我的代码编辑版本,上面的代码有一些修改:

require 'open3'
puts"Enter the command for execution"
some_command=gets
stdout,stderr,status = Open3.capture3(some_command)
STDERR.puts stderr
if status.success?
  puts stdout
else
  STDERR.puts "ERRRR"
end