2011-07-06 48 views
4

安装在一个行示例RVM:ruby​​在一行中执行远程脚本。 (如安装RVM)

user$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm) 

现在,说我在http://blah.com/helloworld.rb

puts "what's ur name?" 
name = gets.chomp 
puts "hello world from web, #{name}" 

有一个Ruby脚本这样我想实现这一点,在我的壳而无需在一行中创建临时文件,甚至不需要创建一个命令。

wget http://blah.com/helloworld.rb; ruby helloworld.rb; rm helloworld.rb 

我试过这个,但用户提示会因为先前的管道而被忽略。

curl -s http://blah.com/helloworld.rb | ruby 

执行远程ruby脚本的正确方法是什么?谢谢!

回答

6

像这样:

ruby < <(curl -s http://blah.com/helloworld.rb) 

红宝石类似的评估Ruby代码来砸如何评估shell代码

+0

第一个'<'是不必要的。 – cam

+0

这似乎并没有在这里工作。 – c2h2

+0

尝试添加一个<(代码更新) –

5

基于口径另一个Ruby选项安装shell脚本:

ruby -e "require 'open-uri'; system open('http:// or local file').read" 

同为Ruby脚本:

ruby -e "require 'open-uri'; eval open('http:// or local file').read" 

编辑:固定失踪报价,并添加Ruby脚本执行

+0

我不认为这个工程。 – c2h2

+0

你是对的,我忘了一个报价,并改变它与ruby脚本一起工作。我要编辑它来修复它。谢谢 – rgo

0

在Ruby代码,你必须重新打开标准输入,并将其绑到控制终端设备/dev/tty

rubyscript="$(cat <<-'EOF' 
puts "what's ur name?" 
name = gets.chomp 
puts "hello world from web, #{name}" 
EOF 
)" 

ruby <(echo '$stdin.reopen(File.open("/dev/tty", "r"))'; echo "$rubyscript")