解释脚本:隐式执行与显式执行
在这个超级用户问题中,我被建议最好执行用以下语言编写的脚本通过显式执行解释器并提供脚本作为参数来解释的语言(php、python 等),例如:
> php script.php
而不是在脚本中添加一行来告诉操作系统执行它,例如:
#!/usr/bin/php
<?php
echo "hello world";
?>
为什么这是真的?我的直觉告诉我,如果脚本被移动到解释器的可执行文件位于不同路径的系统中,这会更安全,但这是唯一的原因吗?
In this superuser question I was advised that it is better to execute scripts written in an interpretted language (php, python, etc) by explicitly executing the interpretter and providing the script as an argument, like:
> php script.php
rather than adding a line to the script to tell the OS to execute it, like:
#!/usr/bin/php
<?php
echo "hello world";
?>
Why is this true? My intuition tells me that it's safer, in case the script is moved to a system in which the interpreter's executable is located at a different path, but is that the only reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用这个习惯用法,可移植性会得到增强:
但它也有自己的缺点;如需查看更长的讨论,请访问 http://sites.google.com/site/frankpzh /知识库/shebang
Portability is enhanced if you use this idiom:
but it has drawbacks of its own; see a longer discussion at http://sites.google.com/site/frankpzh/knowledge-library/shebang
不同的路径可能是主要原因,尤其是当二进制文件开始存储在 x64 表示的路径中或安装在 /usr/local/bin/php 中时。
Different paths would be the primary reason, especially when binaries start getting stored in x64-denoted paths or installed in /usr/local/bin/php.