#!/usr/bin/env的问题

80 篇文章 0 订阅
64 篇文章 1 订阅
#!/usr/bin/php          是告诉操作系统执行这个脚本的时候,调用/usr/bin下的php解释器;
#!/usr/bin/env php   这种用法是为了防止操作系统用户没有将php装在默认的/usr/bin路径里。当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。
 最近在写PHP脚本发现有些同事脚本有这么个写法,长见识了,google了一下,弄明白啥意思了分享下:
    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
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anssummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值