码迷,mamicode.com
首页 > 其他好文 > 详细

#!/usr/bin/env 脚本解释程序的作用

时间:2018-01-03 22:34:51      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:specified   inter   ems   inf   col   mes   php   svn   ble   

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 脚本解释程序的作用

标签:specified   inter   ems   inf   col   mes   php   svn   ble   

原文地址:https://www.cnblogs.com/oxspirt/p/8185550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!