如何使用shebang(#!)来使用正确的Ruby版本

时间:2016-01-11 15:16:13

标签: ruby bash rvm

我正在使用Sensu对Sensu客户端运行检查。

由于Ruby版本小于2.0,我收到此错误:

rvm use 2.1.8
Using /home/jenkins/.rvm/gems/ruby-2.1.8

我使用RVM安装了Ruby 2.1.8:

#!

如何强制互操作者使用正确的版本?

我尝试以各种方式使用文件顶部的shebang(#!/home/jenkins/.rvm/gems/ruby-2.1.8 ruby):

  1. #!/usr/bin/env rvm 2.0 do ruby
  2. How to set the correct shebang for the needed Ruby version”无效=> source "/home/jenkins/.rvm/scripts/rvm" rvm use "2.1.8"
  3. 脚本中的
  4. jenkins@chef-production2-backoffice01:~$ rvm use 2.1.8
    Using /home/jenkins/.rvm/gems/ruby-2.1.8
    jenkins@chef-production2-backoffice01:~$ ruby check_disk_space.rb #works 
    CheckDisk OK: All disk usage under 85% and inode usage under 85%
    jenkins@chef-production2-backoffice01:~$ rvm use 1.8.7
    Using /home/jenkins/.rvm/gems/ruby-1.8.7-head
    jenkins@chef-production2-backoffice01:~$ ruby check_disk_space.rb #doesn't work even though first row is #!/home/jenkins/.rvm/rubies/ruby-2.1.8/bin/ruby
    check_disk_space.rb:32: syntax error, unexpected ':', expecting kEND
             short: '-t TYPE[,TYPE]',
                   ^
    check_disk_space.rb:32: syntax error, unexpected ',', expecting kEND
    
  5. 还有其他建议吗?

    修改 感谢@mudasobwa,当我使用2.1.8

    从jenkins用户运行它时,它正在工作
    sensu

    另一个问题是运行脚本的程序是与已知用户 ps aux |grep sensu sensu 13148 4.0 0.4 86512 18696 ? Sl 09:38 0:00 /opt/sensu/embedded/bin/ruby /opt/sensu/bin/sensu-client -b -c /etc/sensu/config.json ... 一起运行的sensu客户端 if(AuthorizedForRole[someAction]==true) { [some code] }else{ Return "Unauthorized access attept"; }

1 个答案:

答案 0 :(得分:0)

您滥用shebang行格式。应该有which ruby返回的可执行路径。

因此,首选方法是使用此Ruby作为标准默认值:

rvm use 2.1.8 --default

这应该足以使用所选的Ruby版本运行Ruby脚本。如果要为此特定脚本选择此版本,最简单的方法是在使用which ruby切换版本后运行rvm,并在shebang行中复制粘贴上面的输出:

$ which ruby
/home/jenkins/.rvm/rubies/ruby-2.1.8/bin/ruby # NB the output may differ

现在按原样在shebang中使用它,没有空格:

#!/home/jenkins/.rvm/rubies/ruby-2.1.8/bin/ruby