我是红宝石的新手。我试图在ruby中获取我的shell脚本并在获取的shell脚本中执行功能。
下面是我的Shell脚本 /tmp/test.sh
#!/bin/bash
function hello {
echo "hello, this script is being called from ruby"
}
下面是我的红宝石脚本 test.rb
#!/usr/bin/ruby
system("source /tmp/test.sh")
puts $?.exitstatus
system("hello")
puts $?.exitstatus
使用 system
[root@localhost ~]# ruby test.rb
127
127
我什至尝试了反向滴答方法,但是我遇到了错误
代码:
#!/usr/bin/ruby
status=`source /root/test.sh`
puts status
status2=`hello`
puts status2
错误:
ruby test.rb
test.rb:3:in ``': No such file or directory - source (Errno::ENOENT)
from test.rb:3:in `<main>'
谁能告诉我代码中的错误。
答案 0 :(得分:1)
您可以使用session gem,或自己编写解决方案。
script.sh:
#!/bin/bash
function hello() {
echo "Hello, World!"
}
Ruby文件:
IO.popen('bash', 'r+') do |sh|
sh.puts 'source script.sh'
sh.puts 'hello'
sh.close_write
puts sh.gets
end
# => Hello, World!