为什么 ruby​​ 的 '$:'或“$LOAD_PATH”当我用 sudo 运行它时会改变吗?

发布于 2024-09-09 04:28:49 字数 2764 浏览 8 评论 0原文

我正在尝试以 root 身份运行 ruby​​ 脚本。当我尝试 require 一个 gem 时,ruby 说找不到它。这是因为 $:

$ ruby -e "puts $:"
/opt/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/cgi_multipart_eof_fix-2.5.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/daemons-1.1.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/lib
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/ext
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/bin
/opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/bin
/opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/bin
/opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/lib
/opt/local/lib/ruby/gems/1.9.1/gems/msgpack-0.4.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/nestful-0.0.2/lib
/opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/bin
/opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/lib
/opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/ext
/opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/bin
/opt/local/lib/ruby/gems/1.9.1/gems/open4-1.0.1/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib
/opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/bin
/opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/lib
/opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/bin
/opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/sexp_processor-3.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/sinatra-1.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib
/opt/local/lib/ruby1.9/site_ruby/1.9.1
/opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/site_ruby
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/vendor_ruby
/opt/local/lib/ruby1.9/1.9.1
/opt/local/lib/ruby1.9/1.9.1/i386-darwin10
.
$ sudo ruby -e "puts $:"
/opt/local/lib/ruby1.9/site_ruby/1.9.1
/opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/site_ruby
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/vendor_ruby
/opt/local/lib/ruby1.9/1.9.1
/opt/local/lib/ruby1.9/1.9.1/i386-darwin10
.

为什么会发生这种情况?

I am trying to run a ruby script as root. When I try to require a gem, ruby says it can't be found. This is because of $:

$ ruby -e "puts $:"
/opt/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/cgi_multipart_eof_fix-2.5.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/daemons-1.1.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/lib
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/ext
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/bin
/opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/bin
/opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/bin
/opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/lib
/opt/local/lib/ruby/gems/1.9.1/gems/msgpack-0.4.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/nestful-0.0.2/lib
/opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/bin
/opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/lib
/opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/ext
/opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/bin
/opt/local/lib/ruby/gems/1.9.1/gems/open4-1.0.1/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib
/opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/bin
/opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/lib
/opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/bin
/opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/sexp_processor-3.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/sinatra-1.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib
/opt/local/lib/ruby1.9/site_ruby/1.9.1
/opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/site_ruby
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/vendor_ruby
/opt/local/lib/ruby1.9/1.9.1
/opt/local/lib/ruby1.9/1.9.1/i386-darwin10
.
$ sudo ruby -e "puts $:"
/opt/local/lib/ruby1.9/site_ruby/1.9.1
/opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/site_ruby
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/vendor_ruby
/opt/local/lib/ruby1.9/1.9.1
/opt/local/lib/ruby1.9/1.9.1/i386-darwin10
.

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2024-09-16 04:28:49

尝试 sudo -E ruby​​ -e "puts $:" 。

sudo 手册页:

   -E          The -E (preserve environment) option will override the env_reset option in sudoers(5)).
               It is only available when either the matching command has the SETENV tag or the setenv
               option is set in sudoers(5).

Try sudo -E ruby -e "puts $:".

From the sudo man page:

   -E          The -E (preserve environment) option will override the env_reset option in sudoers(5)).
               It is only available when either the matching command has the SETENV tag or the setenv
               option is set in sudoers(5).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文