2010-07-11 19 views
0

J'essaie d'exécuter un script ruby ​​en tant que root. Quand j'essaie de trouver une gemme, ruby ​​dit qu'elle ne peut pas être trouvée. Ceci est dû à $:

$ 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 
. 

Pourquoi est-ce qui se passe?

+0

Est-ce votre '$ path' le même pour l'utilisateur régulier et l'utilisateur sudo'd? – theIV

+0

@theIV: Oui - 'echo $ PATH' et' sudo echo $ PATH' ont la même sortie. C'est ce que vous vouliez dire? – Adrian

Répondre

3

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

De la page man 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). 
+0

Cela le corrige. Comment puis-je faire en sorte que je n'ai pas à taper le '-E'? – Adrian

+1

Pourriez-vous élaborer un peu sur ce qui se passe dans la ligne ci-dessus? Cependant, cela commence à ressembler à une question SuperUser. – theIV

+0

@Adrian: 'echo" alias sudo = 'sudo -E' ">> ~/.bashrc' – amphetamachine