Pregunta

Escribí una envoltura de bash a Ruby que pasa por varios pasos de configuración.

La versión más básica es,

   #!/bin/bash 
   #   ruby_wrapper.sh
   ruby

¡Ahora quiero poder usar este envoltorio como Ruby regular!Específicamente, quiero crear un archivo .RB que use este "intérprete".

   #!/path/to/ruby_wrapper.sh
   #  my_file.rb
   puts "hello world"

Entonces quiero poder hacer $ ./my_file.rb en lugar de $ ruby_wrapper.sh my_file.rb

es esto posible?

the Documentación afirma que es 't.

Tenga en cuenta que el intérprete puede ser no ser un script de intérprete.

pero no veo por qué no.¿Alguien tiene alguna idea para evitar esto?

¿Fue útil?

Solución

Try invoking your wrapper with /usr/bin/env. It's actually good practice to execute Ruby scripts with /usr/bin/env ruby as you don't have to hard code the path to the ruby binary, so this is not unnatural.

$ cat ruby_wrapper.sh 
#!/bin/bash
exec ruby "$@"

$ cat wrapped.rb 
#!/usr/bin/env /tmp/ruby_wrapper.sh
puts "hello world"

$ ./wrapped.rb 
hello world

Also as a side note see how I've used exec in the wrapper script. This will allow the ruby interpreter to take over your wrapper script's process rather than run as a child process of your script. This makes the wrapper transparent to the caller (signals will be delivered directly to ruby rather than to bash, for example).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top