首页 > 其他 > 详细

how to execute-shell-commands by ruby

时间:2014-06-12 06:49:39      阅读:374      评论:0      收藏:0      [点我收藏+]

 

 

Execute shell commands

There are a number of different ways to run shell commands from Ruby.

The exec command

Kernel#exec replaces the current process and runs the command:

exec(ls ~)
# Nothing after this command is executed

This might be a bit impractical, so have a look at the other options.

Backticks or %x shortcut

Place your command inside backticks (`) or execute it within %x() and it will return the output of this command:

`ls ~`
=> "Applications\nDesktop\nDocuments"
%x(ls ~)
=> "Applications\nDesktop\nDocuments"

The system command

Use Kernel#system and it will return true (command run successfully), false (unsuccessful) or nil (command execution failed):

system(ls ~)
=> true

For all of these methods, you can access the PID and exit status of the unix process via the$? variable:

how to execute-shell-commands by ruby,布布扣,bubuko.com

how to execute-shell-commands by ruby

原文:http://www.cnblogs.com/iwangzheng/p/3782740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!