【问题标题】:Accessing the return value of a ruby script in shell在 shell 中访问 ruby​​ 脚本的返回值
【发布时间】:2014-02-07 10:20:30
【问题描述】:

如何在test1.rb 中访问变量a 的值,以便在test1.rb 外部SSH 中进一步使用?

test1.rb

Net::SSH.start("host", ava) do |ssh|
   ssh.exec('ruby test2.rb')
end

hosttest2.rb

#!/usr/bin/env ruby

class Value
  def get_value()
    a = 1 + 2
  end
end

v = Value.new
v.get_value()

【问题讨论】:

  • puts v.get_value().
  • 您可以设置返回码退出:exit v.get_value()
  • 我想在 test1.rb 的其余部分使用 a 的值

标签: ruby shell ssh terminal


【解决方案1】:

在您的 test1.rb 脚本中,您需要将一个块传递给 ssh.exec 方法,并且在该块中您将能够从 test2.rb 访问您 puts 的任何内容

Net::SSH.start("127.0.0.1", 'ava', password: '') do |ssh|
  ssh.exec('ruby test2.rb') do |channel, stream, data|
    puts "got: #{data}" if stream == :stdout
  end
end

puts你想从test2.rb传给test1.rb的值:

v = Value.new
puts v.get_value()

这会将值发送到标准输出

自述文件中的更多信息:https://github.com/net-ssh/net-ssh#readme

【讨论】:

    猜你喜欢
    • 2015-03-20
    • 2013-12-11
    • 2013-07-16
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多