2014-09-24 58 views
0

全部,使用rvm ruby​​-2.1.2但使用rvm jruby-1.7.12的控制檯的腳本。腳本失敗

我不認爲這是通過偵察後問這裏的問題。這裏是我的問題:

我使用Ubuntu 14.04。我開發了一個由ruby-2.1.2運行的腳本。我已經在腳本頭文件中包含了shebang行

#!/usr/bin/env ruby 

腳本路徑包含在.profile中。腳本通過chmod + x可執行。該腳本可以在任何地方執行沒有問題。我RVM -v輸出

rvm 1.25.27 (stable) by Wayne E. Seguin, Michal Papis [https://rvm.io/] 

但是今天,我開始開發使用JRuby-1.7.12,我已經RVM從命令行JRuby的。突然劇本不再工作了。該腳本現在使用jruby-1.7.12而不是ruby-2.1.2運行。這絕對不是我想要的。

一次嘗試

shebang行更改爲

#!/usr/bin/env rvm ruby-2.1.2 do ruby 

用「的/ usr/bin中/ env的外殼迴應:RVM紅寶石2.1.2不紅寶石:沒有這樣的文件或目錄「

第二次嘗試

通過改變家當裏再次嘗試網元

#!/home/xxx/.rvm/rubies/ruby-2.1.2/bin/ruby 

現在不是運行該腳本,輸出顯示其編譯爲本地模塊,JRuby的環境下:

/home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:89:in `run': ERROR: Failed to build gem native extension. (Gem::Ext::BuildError) 

/home/xxx/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb 
extconf.rb:4:in `<main>': Use RbConfig instead of obsolete and deprecated Config. 
extconf.rb:4:in `<main>': Use RbConfig instead of obsolete and deprecated Config. 
extconf.rb:5:in `<main>': Use RbConfig instead of obsolete and deprecated Config. 
# These line gets substituted with the actual Config::CONFIG items location by extconf.rb 
PREFIX = /home/xxx/.rvm/rubies/ruby-2.1.2 
BINDIR = /home/xxx/.rvm/rubies/ruby-2.1.2/bin 
INSTALLDIR = $(PREFIX)/lib/ruby/shared/rubygems/defaults 
OLDINSTALLDIR = $(PREFIX)/lib/ruby/site_ruby/1.8/rubygems/defaults 

ifeq (true,$(shell test -x $(BINDIR)/jruby && echo true)) 
RAKE=$(BINDIR)/jruby -S rake 
else 
RAKE=rake 
endif 

build: .build-post 

.build-pre: 

.build-post: .build-impl build-exe 

build-exe: 
    @if [ "$(findstring mingw, $(CONF))" ]; then           \ 
    ${MAKE} -f ${SUB_CONFMK} SUBPROJECTS=${SUBPROJECTS} jruby.exe jrubyw.exe;     \ 
    if [ -d ../jruby ]; then cp jruby.exe jrubyw.exe jruby.dll ../jruby/bin/; fi;     \ 
    if [ -d D:/work/jruby-dev/jruby ]; then cp jruby.exe jrubyw.exe jruby.dll D:/work/jruby-dev/jruby/bin/; fi; \ 
    fi 

jruby.res: resources/jruby.rc 
    windres $^ -O coff -o [email protected] 

jruby.exe: jrubyexe.cpp nbexecloader.h utilsfuncs.cpp utilsfuncswin.cpp jruby.res 
    g++ $(CXXFLAGS) $^ -s -o [email protected] $(LDLIBSOPTIONS) -static 

jrubyw.exe: jrubyexe.cpp nbexecloader.h utilsfuncs.cpp utilsfuncswin.cpp jruby.res 
    g++ $(CXXFLAGS) -DJRUBYW -mwindows $^ -s -o [email protected] $(LDLIBSOPTIONS) -static 

install: 
    @if [ ! -f ./jruby ]; then echo "Please run 'make' first."; exit 1; fi 
    @if [ x$(BINDIR) = xnotspecified/bin ]; then echo "Please define where to install by passing PREFIX=<jruby-home>."; exit 1; fi 
    @if [ ! -w $(BINDIR) ]; then echo "'$(BINDIR)' does not exist or cannot write to '$(BINDIR)'."; exit 1; fi 
    @if [ -f $(BINDIR)/jruby -a ! -w $(BINDIR)/jruby ]; then echo "Cannot write to '$(BINDIR)/jruby'."; exit 1; fi 
    cp ./jruby $(BINDIR)/jruby 
    @if [ x$(PREFIX) = xnotspecified ]; then echo "Please define where to install by passing PREFIX=<jruby-home>."; exit 1; fi 
    @if [ ! -w $(INSTALLDIR) ]; then \ 
     if [ ! -w $(OLDINSTALLDIR) ]; then \ 
      echo "Neither '$(INSTALLDIR)' nor '$(OLDINSTALLDIR)' exist and are writable"; exit 1; \ 
     else \ 
      echo "cp ./lib/rubygems/defaults/jruby_native.rb $(OLDINSTALLDIR)"; \ 
      cp ./lib/rubygems/defaults/jruby_native.rb $(OLDINSTALLDIR); \ 
     fi; \ 
    else \ 
     echo "cp ./lib/rubygems/defaults/jruby_native.rb $(INSTALLDIR)"; \ 
     cp ./lib/rubygems/defaults/jruby_native.rb $(INSTALLDIR); \ 
    fi; 

test: 
    $(RAKE) 

# Universal binary on OSX 
FAT_ARCHES=i386 ppc x86_64 

fat: $(FAT_ARCHES) 
    lipo -create $(foreach arch,$(FAT_ARCHES),build/unix/Darwin-$(arch)/jruby-launcher) -output jruby 
    $(RAKE) 

$(FAT_ARCHES): 
    $(MAKE) -f $(SUB_CONFMK) [email protected] CFLAGS="-arch [email protected]" build/unix/[email protected]/jruby-launcher 

clean: .clean-post 

.clean-pre: 
    -rm -rf build/* 

.clean-post: .clean-impl 
    rm -f *.exe *.res 

clobber: .clobber-post 

.clobber-pre: 

.clobber-post: .clobber-impl 

all: .all-post 

.all-pre: 

.all-post: .all-impl 

help: .help-post 

.help-pre: 

.help-post: .help-impl 

# Use the manually-maintained inc/*.mk makefiles. 
# Pass NETBEANS=true on the command-line to use NB's generated 
# nbproject/*.mk 

ifdef NETBEANS 
SUB_IMPLMK=nbproject/Makefile-impl.mk 
else 
SUB_IMPLMK=inc/Makefile-impl.mk 
SUB_CONFMK=inc/Makefile-rules.mk 
endif 

# include project implementation makefile 
include $(SUB_IMPLMK) 

# Pick conf based on OS. for mingw64, must manually override for now. 
ifeq ($(OS),Windows_NT) 
CONF=mingw 
else 
CONF=unix 
endif 

ifdef NETBEANS 
SUB_CONFMK=nbproject/Makefile-${CONF}.mk 
endif 

make "DESTDIR=" clean 
rm -rf build/* 
make -f inc/Makefile-rules.mk CONF=unix SUBPROJECTS= .clean-conf 
make[1]: Entering directory `/home/xxx/.rvm/rubies/jruby-1.7.12/lib/ruby/gems/shared/gems/jruby-launcher-1.1.0-java' 
rm -f -r build/unix 
rm -f -f jruby 
make[1]: Leaving directory `/home/xxx/.rvm/rubies/jruby-1.7.12/lib/ruby/gems/shared/gems/jruby-launcher-1.1.0-java' 
rm -f *.exe *.res 

make "DESTDIR=" 
make -f inc/Makefile-rules.mk CONF=unix SUBPROJECTS= .build-conf 
make[1]: Entering directory `/home/xxx/.rvm/rubies/jruby-1.7.12/lib/ruby/gems/shared/gems/jruby-launcher-1.1.0-java' 
make -f inc/Makefile-rules.mk jruby 
make[2]: Entering directory `/home/xxx/.rvm/rubies/jruby-1.7.12/lib/ruby/gems/shared/gems/jruby-launcher-1.1.0-java' 
mkdir -p build/unix/Linux 
rm -f build/unix/Linux/argparser.o.d 
g++ -O2 -Wall -c argparser.cpp -MMD -MP -MF build/unix/Linux/argparser.o.d -o build/unix/Linux/argparser.o 
argparser.cpp: In member function ‘bool ArgParser::initPlatformDir()’: 
argparser.cpp:134:40: warning: converting to non-pointer type ‘char’ from NULL [-Wconversion-null] 
     char sep[2] = { FILE_SEP, NULL }; 
             ^
argparser.cpp:191:58: warning: ignoring return value of ‘char* getcwd(char*, size_t)’, declared with attribute warn_unused_result [-Wunused-result] 
     getcwd(path, PATH_MAX - platformDir.length() - 1); 
                 ^
mkdir -p build/unix/Linux 
rm -f build/unix/Linux/utilsfuncs.o.d 
g++ -O2 -Wall -c utilsfuncs.cpp -MMD -MP -MF build/unix/Linux/utilsfuncs.o.d -o build/unix/Linux/utilsfuncs.o 
utilsfuncs.cpp: In function ‘std::string findOnPath(const char*)’: 
utilsfuncs.cpp:126:12: warning: unused variable ‘found’ [-Wunused-variable] 
    char * found; 
      ^
mkdir -p build/unix/Linux 
rm -f build/unix/Linux/ng.o.d 
gcc -O2 -Wall -c ng.c -MMD -MP -MF build/unix/Linux/ng.o.d -o build/unix/Linux/ng.o 
mkdir -p build/unix/Linux 
rm -f build/unix/Linux/strlcpy.o.d 
gcc -O2 -Wall -c strlcpy.c -MMD -MP -MF build/unix/Linux/strlcpy.o.d -o build/unix/Linux/strlcpy.o 
mkdir -p build/unix/Linux 
rm -f build/unix/Linux/jrubyexe.o.d 
g++ -O2 -Wall -c jrubyexe.cpp -MMD -MP -MF build/unix/Linux/jrubyexe.o.d -o build/unix/Linux/jrubyexe.o 
mkdir -p build/unix/Linux 
rm -f build/unix/Linux/unixlauncher.o.d 
g++ -O2 -Wall -c unixlauncher.cpp -MMD -MP -MF build/unix/Linux/unixlauncher.o.d -o build/unix/Linux/unixlauncher.o 
g++ -O2 -Wall  -o build/unix/Linux/jruby-launcher build/unix/Linux/argparser.o build/unix/Linux/utilsfuncs.o build/unix/Linux/ng.o build/unix/Linux/strlcpy.o build/unix/Linux/jrubyexe.o build/unix/Linux/unixlauncher.o -lstdc++ 
cp build/unix/Linux/jruby-launcher jruby 
make[2]: Leaving directory `/home/xxx/.rvm/rubies/jruby-1.7.12/lib/ruby/gems/shared/gems/jruby-launcher-1.1.0-java' 
make[1]: Leaving directory `/home/xxx/.rvm/rubies/jruby-1.7.12/lib/ruby/gems/shared/gems/jruby-launcher-1.1.0-java' 

make "DESTDIR=" install 
cp ./jruby /home/xxx/.rvm/rubies/ruby-2.1.2/bin/jruby 
Neither '/home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/shared/rubygems/defaults' nor '/home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/1.8/rubygems/defaults' exist and are writable 
make: *** [install] Error 1 

make install failed, exit code 2 

Gem files will remain installed in /home/xxx/.rvm/gems/[email protected]/gems/jruby-launcher-1.1.0-java for inspection. 
Results logged to /home/xxx/.rvm/gems/[email protected]/extensions/x86_64-linux/2.1.0/jruby-launcher-1.1.0-java/gem_make.out 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:51:in `block in make' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:43:in `each' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:43:in `make' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/ext_conf_builder.rb:47:in `block in build' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tempfile.rb:324:in `open' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/ext_conf_builder.rb:17:in `build' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:161:in `block (2 levels) in build_extension' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:160:in `chdir' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:160:in `block in build_extension' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:159:in `synchronize' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:159:in `build_extension' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:198:in `block in build_extensions' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:195:in `each' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/ext/builder.rb:195:in `build_extensions' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/specification.rb:1436:in `block in build_extensions' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/user_interaction.rb:45:in `use_ui' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/specification.rb:1434:in `build_extensions' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/stub_specification.rb:60:in `build_extensions' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/basic_specification.rb:56:in `contains_requirable_file?' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/specification.rb:925:in `block in find_inactive_by_path' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/specification.rb:924:in `each' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/specification.rb:924:in `find' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/specification.rb:924:in `find_inactive_by_path' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems.rb:185:in `try_activate' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:132:in `rescue in require' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require' 
    from /home/xxx/Tools/DogFood/TrackerClient/client/webservices.rb:2:in `<top (required)>' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /home/xxx/Tools/DogFood/TrackerClient/dvcs/git/git.rb:4:in `<top (required)>' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /home/xxx/Tools/DogFood/TrackerClient/dvcs/dvcs.rb:2:in `<top (required)>' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /home/xxx/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
    from /home/xxx/Tools/DogFood/TrackerClient/tc2.rb:10:in `<main>' 

我相信還有其他的環境價值需求爲改變這種GEM_PATH和RUBY_VERSION如本網站發佈的其他信息所示。這不是最好的。

第三次嘗試

找到名爲RVM-家當 - 紅寶石RVM的bin文件夾的工具。改變shebang到

#!/usr/bin/env rvm-shebang-ruby 

而是運行腳本,它執行ruby並在控制檯上等待。

我現在可以通過其他方式實現我想要的功能: 嚴格運行腳本,使用ruby-2.1.2和ruby 2.1.2在RVM環境中。

謝謝!

+0

不知道什麼是錯的,我在安裝'#在/ usr/bin中/ env的RVM JRuby的1.7.11做如預期的那樣,ruby'跟隨'puts JRUBY_VERSION'打印'1.7.11'。如果我把'ruby-2.1.1'和'RUBY_VERSION'放在一起,我就再次得到正確的'2.1.1'。 – Amadan 2014-09-24 04:28:58

+0

如果我用ruby執行文件,是的,它可以工作。但是,如果我使文件成爲可執行文件並直接從命令行執行,則不會失敗。 – ChrisLiaw 2014-09-24 04:35:34

回答

0

好的,您的第一個示例(我認爲這是正確的)適用於我的系統,因爲我在OSX上。顯然,這不是一種便攜式語法。

啊,我明白了。我在OSX上,顯然有env,工作方式不同。From Wikipedia on Shebang

另一個可移植性問題是對命令參數的解釋。一些系統中,包括Linux,不分裂的參數; [11]例如,運行與所述第一線等的腳本,

#!/usr/bin/env python -c 

即當,蟒-c將被作爲一個參數/傳遞usr/bin/env,而不是兩個參數。 Cygwin也以這種方式表現。

這裏是一個黑客,你可以做,而不是:

#!/usr/bin/env ruby 

unless RUBY_VERSION == '2.1.2' 
    Process.exec('rvm', 'ruby-2.1.2', 'do', $0, *ARGV) 
end 

puts RUBY_VERSION 

這將批,加載文件兩次,並加載解釋兩次。在第一次加載的時候,你的文件中也不能有任何破壞語法的東西,所以新的東西都應該被卸載到需求。我對此並不滿意。

更好,更可讀的想法是有一個shell腳本包裝:

#!/bin/sh 
rvm ruby-2.1.2 do script.rb 
+0

你的「更好的方法」是我最終做的。我仍然希望有另一種方式。似乎這是要走的路。謝謝! – ChrisLiaw 2014-09-24 05:52:53