ruby在一行中执行远程脚本。 (比如安装rvm)

时间:2011-07-06 13:17:02

标签: ruby linux bash shell

在一行示例中安装rvm:

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

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

上有这样的ruby脚本
puts "what's ur name?"
name = gets.chomp
puts "hello world from web, #{name}"

我想在我的shell中实现它,而不是在一行甚至更好的一个命令中创建临时文件。

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

我试过这个,但由于早期的管道,用户提示将被忽略。

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

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

4 个答案:

答案 0 :(得分:6)

像这样:

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

Ruby类似于bash评估shell代码的方式评估ruby代码

答案 1 :(得分:5)

基于用于shell脚本的Caliber安装的另一个Ruby选项:

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脚本执行

答案 2 :(得分:4)

来自http://brew.sh/

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

答案 3 :(得分:0)

在您的Ruby代码中,您必须重新打开stdin并将其绑定到控制终端设备/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")