如何从Ruby程序内部调用shell命令?然后如何将这些命令的输出返回到Ruby中?
当前回答
这不是一个真正的答案,但也许有人会发现它有用:
在Windows上使用TK GUI时,您需要从rubyw调用shell命令,您总是会有一个烦人的CMD窗口弹出不到一秒钟。
为了避免这种情况,你可以使用:
WIN32OLE.new('Shell.Application').ShellExecute('ipconfig > log.txt','','','open',0)
or
WIN32OLE.new('WScript.Shell').Run('ipconfig > log.txt',0,0)
两者都将ipconfig输出存储在log.txt中,但是不会弹出窗口。
你需要在你的脚本中要求'win32ole'。
system(), exec()和spawn()都会在使用TK和rubyw时弹出恼人的窗口。
其他回答
给定像attrib这样的命令:
require 'open3'
a="attrib"
Open3.popen3(a) do |stdin, stdout, stderr|
puts stdout.read
end
我发现,虽然这种方法不像
system("thecommand")
or
`thecommand`
在反引号中,与其他方法相比,此方法的一个优点是 反勾号似乎不让我把我运行的命令/存储我想要运行的命令在一个变量中,而system(" command")似乎不让我得到输出,而这个方法让我做这两件事,它让我访问stdin, stdout和stderr独立。
参见“在ruby中执行命令”和ruby的Open3文档。
如果您有一个比普通情况更复杂的情况,不能用' '来处理,那么请检查Kernel.spawn()。这似乎是Ruby提供的用于执行外部命令的最通用/功能最齐全的程序。
你可以用它来:
创建进程组(Windows)。 重定向进,出,错误文件/彼此。 设置env vars, umask。 执行命令前请更换目录。 设置CPU/数据等资源限制。 在其他答案中做其他选项可以做的所有事情,但需要更多的代码。
Ruby文档中有足够好的例子:
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)
我绝对不是Ruby专家,但我还是会试一试:
$ irb
system "echo Hi"
Hi
=> true
你还应该能够做以下事情:
cmd = 'ls'
system(cmd)
你也可以使用反勾操作符('),类似于Perl:
directoryListing = `ls /`
puts directoryListing # prints the contents of the root directory
如果你需要简单的东西,很方便。
你想要使用哪种方法取决于你想要完成什么;有关不同方法的更多细节,请查看文档。
下面是我在OS X上的ruby脚本中使用的一个很酷的脚本(这样我就可以在切换离开窗口后启动脚本并获得更新):
cmd = %Q|osascript -e 'display notification "Server was reset" with title "Posted Update"'|
system ( cmd )
推荐文章
- 如何合并2 JSON对象从2个文件使用jq?
- 了解Gemfile。锁文件
- 如何确定一个数组是否包含另一个数组的所有元素
- 是什么导致这个ActiveRecord::ReadOnlyRecord错误?
- 最好的打印散列的方法
- Rails:dependent =>:destroy VS:dependent =>:delete_all
- 我可以在不包含Ruby模块的情况下调用实例方法吗?
- 如何将新项目添加到哈希
- 测试一个Ruby类是否是另一个类的子类
- 什么时候使用Struct vs. OpenStruct?
- 数组到哈希Ruby
- 我如何让红宝石打印一个完整的回溯而不是截断一个?
- 删除Bash脚本中的重复条目
- 如何将Bash命令的输出分配给变量?
- 如何使用RSpec的should_raise与任何类型的异常?