Bash에서 스크립트 인터프리터 작성
문제
다양한 설정 단계를 통과하는 루비에 Bash Wrapper를 썼습니다.
가장 기본적인 버전은 입니다.
#!/bin/bash
# ruby_wrapper.sh
ruby
.
이제는 일반 루비처럼이 래퍼를 사용할 수 있기를 원합니다!특히,이 "인터프리터"를 사용하는 .rb 파일을 만들고 싶습니다.
#!/path/to/ruby_wrapper.sh
# my_file.rb
puts "hello world"
.
$ ./my_file.rb
대신 $ ruby_wrapper.sh my_file.rb
를 수행하고 싶습니다
이이 가능합니까?
the 문서" isn '티.
인터프리터 자체는 인터프리터 스크립트 일 수 있습니다.
그러나 나는 왜 안되는 이유를 보지 못한다.누구 든지이 문제를 해결할 수있는 아이디어가 있습니까?
해결책
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).