如何在Linux中作为Ruby脚本运行命令?

时间:2022-08-10 01:10:33

Let's say I have some terminal commands like:

假设我有一些终端命令,如:

sudo mycommand1
mycommand2
#.....

What should I do run them via ruby script (not bash) in Ubuntu?

我应该怎么做在Ubuntu中通过ruby脚本(而不是bash)运行它们?

UPDATE: I have a ruby script:

更新:我有一个ruby脚本:

def my_method1()
  #calculating something.....
end

def method2(var1, var2)
  #how do I sudo mycommand1 and any other Lunix command from here?
end

def method3(var4)
  #calculating something2....
end

4 个解决方案

#1


1  

You can do system, exec, or place the command in backticks.

您可以执行system,exec或将命令放在反引号中。

exec("mycommand") will replace the current process so that's really only pratical at the end of your ruby script.

exec(“mycommand”)将替换当前进程,因此在ruby脚本结束时真的只是实用。

system("mycommand") will create a new process and return true if the command succeeded and nil otherwise.

system(“mycommand”)将创建一个新进程,如果命令成功则返回true,否则返回nil。

If you need to use the output of your command in your Ruby script use backticks:

如果需要在Ruby脚本中使用命令的输出,请使用反引号:

response = 'mycommand`

#2


1  

There are many questions on SO that answer this. However you can run a command in many ways using system, exec, (backticks), %x{} or using open3. I prefer to use open3 -

SO有很多问题可以回答这个问题。但是,您可以使用system,exec,(反引号),%x {}或使用open3以多种方式运行命令。我更喜欢使用open3 -

require 'open3'

log = File.new("#{your_log_dir}/script.log", "w+")
command = "ls -altr ${HOME}"

Open3.popen3(command) do |stdin, stdout, stderr|
    log.puts "[OUTPUT]:\n#{stdout.read}\n"
    unless (err = stderr.read).empty? then 
        log.puts "[ERROR]:\n#{err}\n"
    end
end

If you want to know more about other options you can refer to Ruby, Difference between exec, system and %x() or Backticks for links to relevant documentation.

如果您想了解更多关于其他选项的信息,可以参考Ruby,exec,system和%x()之间的区别,或者反向信息,以获取相关文档的链接。

#3


1  

You can try these approaches:

您可以尝试以下方法:

  1. %x[command]
  2. Kernel.system"command"
  3. run "command"

#4


0  

make some file.rb with:

制作一些file.rb:

#!/path/to/ruby

system %{sudo mycommand1}
system %{mycommand2}

and the chmod the file with exec permissions (e.g. 755)

并且chmod具有exec权限的文件(例如755)

It you need to pass variables between the two commands, run them together:

你需要在两个命令之间传递变量,一起运行它们:

system %{sudo mycommand1; \
         mycommand2}

#1


1  

You can do system, exec, or place the command in backticks.

您可以执行system,exec或将命令放在反引号中。

exec("mycommand") will replace the current process so that's really only pratical at the end of your ruby script.

exec(“mycommand”)将替换当前进程,因此在ruby脚本结束时真的只是实用。

system("mycommand") will create a new process and return true if the command succeeded and nil otherwise.

system(“mycommand”)将创建一个新进程,如果命令成功则返回true,否则返回nil。

If you need to use the output of your command in your Ruby script use backticks:

如果需要在Ruby脚本中使用命令的输出,请使用反引号:

response = 'mycommand`

#2


1  

There are many questions on SO that answer this. However you can run a command in many ways using system, exec, (backticks), %x{} or using open3. I prefer to use open3 -

SO有很多问题可以回答这个问题。但是,您可以使用system,exec,(反引号),%x {}或使用open3以多种方式运行命令。我更喜欢使用open3 -

require 'open3'

log = File.new("#{your_log_dir}/script.log", "w+")
command = "ls -altr ${HOME}"

Open3.popen3(command) do |stdin, stdout, stderr|
    log.puts "[OUTPUT]:\n#{stdout.read}\n"
    unless (err = stderr.read).empty? then 
        log.puts "[ERROR]:\n#{err}\n"
    end
end

If you want to know more about other options you can refer to Ruby, Difference between exec, system and %x() or Backticks for links to relevant documentation.

如果您想了解更多关于其他选项的信息,可以参考Ruby,exec,system和%x()之间的区别,或者反向信息,以获取相关文档的链接。

#3


1  

You can try these approaches:

您可以尝试以下方法:

  1. %x[command]
  2. Kernel.system"command"
  3. run "command"

#4


0  

make some file.rb with:

制作一些file.rb:

#!/path/to/ruby

system %{sudo mycommand1}
system %{mycommand2}

and the chmod the file with exec permissions (e.g. 755)

并且chmod具有exec权限的文件(例如755)

It you need to pass variables between the two commands, run them together:

你需要在两个命令之间传递变量,一起运行它们:

system %{sudo mycommand1; \
         mycommand2}