首页 > 代码库 > how to execute-shell-commands by ruby
how to execute-shell-commands by ruby
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:
$?.pid => 11988 $?.exitstatus => 0
http://rubyquicktips.com/post/5862861056/execute-shell-commands
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。