码迷,mamicode.com
首页 > 其他好文 > 详细

how to execute-shell-commands by ruby

时间:2014-06-12 06:50:15      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:des   class   code   http   com   html   

 

 

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

标签:des   class   code   http   com   html   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!