如何在ruby中运行sudo命令/调用python

时间:2013-05-26 07:05:07

标签: python ruby

我刚刚开始用Ruby编程,我想知道你是否可以运行像这样的命令 “Sudo python XXX.py”

我尝试使用RubyPython,但没有成功,因为我不知道要检索文件的路径的方式和位置。

有没有人有这方面的经验?

2 个答案:

答案 0 :(得分:1)

  

尝试使用内核模块中的系统方法

     

http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-system

答案 1 :(得分:0)

编辑:试试这个

command_output = `sudo python DIRECTORY`
  p command_output

其中DIRECTORY是您要执行的脚本的完整路径。

有几种方法,每种方法各有不同。

查看此博客文章:http://alvinalexander.com/blog/post/ruby/how-execute-external-shell-command-expansion

# define your variable

dir='/tmp'

# execute your shell command

puts `ls #{dir}`

编辑:看看这个堆栈溢出问题: Running a shell command from Ruby: capturing the output while displaying the output?