Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604719
  • 博文数量: 66
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1667
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-30 11:18
文章分类

全部博文(66)

文章存档

2010年(1)

2008年(65)

我的朋友

分类: LINUX

2008-08-29 17:04:05

今天才知道还有这么个写法,man+了一下,明白是怎么回事了,试着解释如下:
     Probably the most common use of 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
     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变量中定义的目录中寻找。

同样的也适用, #!/usr/bin/php写成 #!/usr/bin/env php会好些,当然更好的是#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} php
阅读(9410) | 评论(1) | 转发(1) |
1

上一篇:深入讨论sed

下一篇:cat,tac与rev命令

给主人留下些什么吧!~~

chinauniixx2014-08-22 23:14:58

写的非常好