質問
私はさまざまな設定ステップを経るRubyへのBashラッパーを書きました。
最も基本的なバージョンは、です
#!/bin/bash
# ruby_wrapper.sh
ruby
.
今私は通常のRubyのようにこのラッパーを使うことができる!具体的には、この「インタプリタ」を使用する.RBファイルを作成します。
#!/path/to/ruby_wrapper.sh
# my_file.rb
puts "hello world"
.
だから私は$ ./my_file.rb
の代わりに$ ruby_wrapper.sh my_file.rb
を実行できるようにしたいです。
これは可能ですか?
Documentation クレームISN 't。
通訳はそれ自体それ自体がインタプリタスクリプトであるかもしれないことに注意してください。
しかし私はなぜわからない。誰かがこれを回避するためのアイデアを持っていますか?
解決
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).