1. 程式人生 > >#!/usr/bin/env 腳本解釋程序的作用

#!/usr/bin/env 腳本解釋程序的作用

ret make bz2 svgz x86_64 scrip 沒有 系統環境 /usr

the Zimbu programming language

http://www.zimbu.org/getting-started

------------------------------------------------------------------------------

#!/usr/bin/env
在linux的一些bash的腳本,需在開頭一行指定腳本的解釋程序,如:
#!/usr/bin/env python
再如:
#!/usr/bin/env perl
#!/usr/bin/env zimbu
#!/usr/bin/env ruby
但有時候也用
#!/usr/bin/python

#!/usr/bin/perl
那麽 env到底有什麽用?何時用這個呢?
腳本用env啟動的原因,是因為腳本解釋器在linux中可能被安裝於不同的目錄,env可以在系統的PATH目錄中查找

。同時,env還規定一些系統環境變量。
如我系統裏env程序執行後打印結果:
[root@int-test bin]# env
rvm_bin_path=/usr/local/rvm/bin
HOSTNAME=int-test
GEM_HOME=/usr/local/rvm/gems/ruby-1.9.3-p484
SHELL=/bin/bash
TERM=linux
HISTSIZE=1000
IRBRC=/usr/local/rvm/rubies/ruby-1.9.3-p484/.irbrc
OLDPWD=/
MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-1.9.3-p484
USER=root
LD_LIBRARY_PATH=:/usr/local/lib:/usr/local/lib:/usr/local/lib
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
_system_type=Linux
rvm_path=/usr/local/rvm
rvm_prefix=/usr/local
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/opt/svn/bin
PWD=/home/git/gitlab-shell/bin
LANG=en_US.UTF-8
_system_arch=x86_64
_system_version=6
HISTCONTROL=ignoredups
rvm_version=1.25.6 (stable)
SHLVL=1
HOME=/root
LOGNAME=root
CVS_RSH=ssh
GEM_PATH=/usr/local/rvm/gems/ruby-1.9.3-p484:/usr/local/rvm/gems/ruby-1.9.3-p484@global
LESSOPEN=|/usr/bin/lesspipe.sh %s
install_flag=1
RUBY_VERSION=ruby-1.9.3-p484
_system_name=CentOS
G_BROKEN_FILENAMES=1
_=/bin/env

可以用env來執行程序:

zhouhh@zhh64:~$ env python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
而如果直接將解釋器路徑寫死在腳本裏,可能在某些系統就會存在找不到解釋器的兼容性問題。有時候我們執行一些腳本時就碰到這種情況。

話說,vim作者Bram Moolenaar推出了一種腳本語言叫zimbu,放在google code上。
地址:http://code.google.com/p/zimbu/
下載編譯後,執行它的示例程序,報錯:


zhouhh@zhh64:~/zimbu$ cat hello.zu
#!/usr/bin/env zimbush

FUNC int MAIN()
IO.write("Hello World!\n")
RETURN 0
}
zhouhh@zhh64:~/zimbu$ ./hello.zu
/usr/bin/env: zimbush: 沒有那個文件或目錄
顯然沒有設置環境變量。

Probably the most common use of env is to find the correct interpreter
for a script, when the interpreter may be in different directories on
different systems. The following example will find the `perl‘ inter-
preter by searching through the directories specified by PATH.

#!/usr/bin/env perl

One limitation of that example is that it assumes the user‘s value for

PATH is set to a value which will find the interpreter you want to exe-
cute. The -P option can be used to make sure a specific list of directo-
ries is used in the search for utility. Note that the -S option is also
required for this example to work correctly.

#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl

The above finds `perl‘ only if it is in /usr/local/bin or /usr/bin. That
could be combined with the present value of PATH, to provide more flexi-
bility. Note that spaces are not required between the -S and -P options:

#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

這種寫法主要是為了讓你的程序在不同的系統上都能適用。
不管你的perl是在/usr/bin/perl還是/usr/local/bin/perl,#!/usr/bin/env perl會自動的在你的用戶PATH變量中所定義的目錄中尋找perl來執行的。

還可以加上-P參數來指定一些目錄去尋找perl這個程序,
#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl的作用就是在/usr/local/bin和/usr/bin目錄下尋找perl。
為了讓程序更加的有可擴展性,可以寫成
#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl,那麽它除了在這兩個目錄尋找之外,還會在PATH變量中定義的目錄中尋找。

同樣的php也適用, #!/usr/bin/php寫成
#!/usr/bin/env php會好些,當然更好的是
#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} php

#!/usr/bin/env 腳本解釋程序的作用