简体   繁体   English

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

[英]How to run sudo command/ call python in ruby

I just started programming in Ruby, I would like to know if you can run a command like "Sudo python XXX.py" 我刚刚开始使用Ruby编程,我想知道您是否可以运行“ Sudo python XXX.py”之类的命令

I tried to work with RubyPython, but without success because I do not know how and where is the path of the file to be retrieved. 我尝试使用RubyPython,但是没有成功,因为我不知道要检索文件路径的方式和位置。

Does anyone have experience with that? 有人有经验吗?

try using system method from kernel module 尝试从内核模块使用系统方法

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

EDIT: try this 编辑:试试这个

command_output = `sudo python DIRECTORY`
  p command_output

Where DIRECTORY is the full path of the script you want to execute. DIRECTORY是要执行的脚本的完整路径。

There are several methods, each with their own differences. 有几种方法,每种方法各有不同。

Check out this blog entry: http://alvinalexander.com/blog/post/ruby/how-execute-external-shell-command-expansion 查看此博客条目: http : //alvinalexander.com/blog/post/ruby/how-execute-external-shell-command-expansion

# define your variable

dir='/tmp'

# execute your shell command

puts `ls #{dir}`

EDIT: Check out this stack overflow question: Running a shell command from Ruby: capturing the output while displaying the output? 编辑:检查此堆栈溢出问题: 从Ruby运行shell命令:在显示输出时捕获输出?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM