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

czfnxgou  于 2023-11-18  发布在  Ruby
关注(0)|答案(2)|浏览(113)

我使用Sensu在Sensu客户端上运行检查。
我得到这个错误,由于Ruby版本低于2.0:

syntax error, unexpected ',', expecting kEND

字符串
我已经使用RVM安装了Ruby 2.1.8:

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


我怎样才能强迫interperter使用正确的版本?
我尝试了在文件顶部使用shebang(#!)的各种方法:

  1. #!/home/jenkins/.rvm/gems/ruby-2.1.8 ruby
    1.“How to set the correct shebang for the needed Ruby version“没有工作=> #!/usr/bin/env rvm 2.0 do ruby
    1.在脚本中:
source "/home/jenkins/.rvm/scripts/rvm"
rvm use "2.1.8"


还有其他建议吗

edit感谢@mudasobwa,当我用2.1.8从jenkins用户运行它时,它可以工作。

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


另一个问题是,运行脚本的程序是sensu客户端,它是由已知用户sensups 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 ...运行的

8ljdwjyq

8ljdwjyq1#

您误用了shebang行格式。应该有可执行文件的路径,如which ruby返回的。
所以,首选的方法是使用这个Ruby作为标准默认值:

rvm use 2.1.8 --default

字符串
这应该足以使用所选的Ruby版本运行Ruby脚本。如果您要为该特定脚本选择此版本,最简单的方法是在将版本切换为rvm后运行which ruby,并将上面的输出复制粘贴到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

pgky5nke

pgky5nke2#

Shebang with rvm:

#! /usr/bin/env ruby

字符串

相关问题