1

I wrote a bash wrapper to ruby that goes through various setup steps.

The most basic version is,

   #!/bin/bash 
   #   ruby_wrapper.sh
   ruby

Now I want to be able to use this wrapper just like regular ruby! Specifically, I want to create a .rb file that uses this "interpreter".

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

So I want to be able to do $ ./my_file.rb instead of $ ruby_wrapper.sh my_file.rb

Is this possible?

The documentation claims it isn't.

Note that the interpreter may not itself be an interpreter script.

But I don't see why not. Does anyone have any ideas to get around this?

diedthreetimes
  • 4,086
  • 26
  • 38
  • The documentation you link and quote from is for NetBSD, not Linux. – Hasturkun May 18 '11 at 22:56
  • True but the same is true on linux. http://www.kernel.org/doc/man-pages/online/pages/man2/execve.2.html >The interpreter must be a valid pathname for an executable which is not itself a script. – diedthreetimes May 18 '11 at 23:07

1 Answers1

9

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Perfect, thanks! I read about the /usr/bin/env trick being used for portability, but didn't realize it would bypass this restriction. – diedthreetimes May 18 '11 at 23:04