Calling shell commands from Ruby

2018-12-31 04:48发布

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

20条回答
残风、尘缘若梦
2楼-- · 2018-12-31 05:10

Here's a flowchart based on this answer. See also, using script to emulate a terminal.

enter image description here

查看更多
人间绝色
3楼-- · 2018-12-31 05:12

Here's a cool one that I use in a ruby script on OS X (so that I can start a script and get an update even after toggling away from the window):

cmd = %Q|osascript -e 'display notification "Server was reset" with title "Posted Update"'|
system ( cmd )
查看更多
妖精总统
4楼-- · 2018-12-31 05:13

I'm definitely not a Ruby expert, but I'll give it a shot:

$ irb 
system "echo Hi"
Hi
=> true

You should also be able to do things like:

cmd = 'ls'
system(cmd)
查看更多
怪性笑人.
5楼-- · 2018-12-31 05:13

The answers above are already quite great, but I really want to share the following summary article: "6 Ways to Run Shell Commands in Ruby"

Basically, it tells us:

Kernel#exec:

exec 'echo "hello $HOSTNAME"'

system and $?:

system 'false' 
puts $?

Backticks (`):

today = `date`

IO#popen:

IO.popen("date") { |f| puts f.gets }

Open3#popen3 -- stdlib:

require "open3"
stdin, stdout, stderr = Open3.popen3('dc') 

Open4#popen4 -- a gem:

require "open4" 
pid, stdin, stdout, stderr = Open4::popen4 "false" # => [26327, #<IO:0x6dff24>, #<IO:0x6dfee8>, #<IO:0x6dfe84>]
查看更多
冷夜・残月
6楼-- · 2018-12-31 05:13

If you have a more complex case than the common case (that can not be handled with ``) then check out Kernel.spawn() here. This seems to be the most generic/full-featured provided by stock Ruby to execute external commands.

E.g. you can use it to:

  • create process groups (Windows)
  • redirect in, out, error to files/each-other.
  • set env vars, umask
  • change dir before executing command
  • set resource limits for CPU/data/...
  • Do everything that can be done with other options in other answers, but with more code.

Official ruby documentation has good enough examples.

env: hash
  name => val : set the environment variable
  name => nil : unset the environment variable
command...:
  commandline                 : command line string which is passed to the standard shell
  cmdname, arg1, ...          : command name and one or more arguments (no shell)
  [cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)
options: hash
  clearing environment variables:
    :unsetenv_others => true   : clear environment variables except specified by env
    :unsetenv_others => false  : dont clear (default)
  process group:
    :pgroup => true or 0 : make a new process group
    :pgroup => pgid      : join to specified process group
    :pgroup => nil       : dont change the process group (default)
  create new process group: Windows only
    :new_pgroup => true  : the new process is the root process of a new process group
    :new_pgroup => false : dont create a new process group (default)
  resource limit: resourcename is core, cpu, data, etc.  See Process.setrlimit.
    :rlimit_resourcename => limit
    :rlimit_resourcename => [cur_limit, max_limit]
  current directory:
    :chdir => str
  umask:
    :umask => int
  redirection:
    key:
      FD              : single file descriptor in child process
      [FD, FD, ...]   : multiple file descriptor in child process
    value:
      FD                        : redirect to the file descriptor in parent process
      string                    : redirect to file with open(string, "r" or "w")
      [string]                  : redirect to file with open(string, File::RDONLY)
      [string, open_mode]       : redirect to file with open(string, open_mode, 0644)
      [string, open_mode, perm] : redirect to file with open(string, open_mode, perm)
      [:child, FD]              : redirect to the redirected file descriptor
      :close                    : close the file descriptor in child process
    FD is one of follows
      :in     : the file descriptor 0 which is the standard input
      :out    : the file descriptor 1 which is the standard output
      :err    : the file descriptor 2 which is the standard error
      integer : the file descriptor of specified the integer
      io      : the file descriptor specified as io.fileno
  file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
    :close_others => false : inherit fds (default for system and exec)
    :close_others => true  : dont inherit (default for spawn and IO.popen)
查看更多
倾城一夜雪
7楼-- · 2018-12-31 05:15

Given a command eg attrib

require 'open3'

a="attrib"
Open3.popen3(a) do |stdin, stdout, stderr|
  puts stdout.read
end

I've found that while this method isn't as memorable as e.g. system("thecommand") or thecommand in backticks, a good thing about this method compared to other methods.. is e.g. backticks doesn't seem to let me 'puts' the command I run / store the command I want to run in a variable, and system("thecommand") doesn't seem to let me get the output. Whereas this method lets me do both of those things, and it lets me access stdin, stdout and stderr independently.

https://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html

http://ruby-doc.org/stdlib-2.4.1/libdoc/open3/rdoc/Open3.html

查看更多
登录 后发表回答