如何添加shebang #! 在 linux 上使用 php 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21731745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add shebang #! with php script on linux?
提问by user2799603
I'm having a little issue with adding shebang #! with my php script on RedHat linux. I have a small piece of test code with shebang added (I've tried different variations as well), but I get the following error message everytime I try to run the script.
我在添加 shebang 时遇到了一个小问题 #! 用我在 RedHat linux 上的 php 脚本。我有一小段添加了shebang的测试代码(我也尝试过不同的变体),但是每次尝试运行脚本时都会收到以下错误消息。
Error msg:
错误消息:
-bash: script.php: command not found
Test script:
测试脚本:
#!/bin/env php
<?php echo "test"; ?>
Shebang #! variations:
舍邦#!变化:
#!/usr/bin/php
#!/usr/bin/env php
采纳答案by Quentin
It should (for most systems) be #!/usr/bin/env php
, but your error isn't related to that.
它应该(对于大多数系统)是#!/usr/bin/env php
,但您的错误与此无关。
-bash: script.php: command not found
-bash: script.php: command not found
It says that script.phpis not found.
它说找不到script.php。
If the problem was the shebang line then the error would say something like:
如果问题出在 shebang 行上,则错误会显示如下内容:
bash: script.php: /usr/env: bad interpreter: No such file or directory
Presumably, you are typing script.php
and the file is not in a directory on your $PATH
or is not executable.
据推测,您正在键入script.php
并且该文件不在您的目录中$PATH
或不可执行。
- Make it executable:
chmod +x script.php
. - Type the path to it instead of just the filename, if it is in the current directory then:
./script.php
.
- 使其可执行:
chmod +x script.php
. - 键入它的路径而不仅仅是文件名,如果它在当前目录中,则:
./script.php
。
Instead of 2, you can move/copy/symlink the file to somewhere listed in $PATH
or modify the $PATH
to include the directory containing the script.
而不是 2,您可以将文件移动/复制/符号链接到列出的某个位置$PATH
或修改$PATH
以包含包含脚本的目录。
回答by Banago
If you script is not located in your /usr/local/bin
and is executable, you have to prefix calling your script with php
like this:
如果您的脚本不在您的目录中/usr/local/bin
并且是可执行的,您必须使用以下前缀调用您的脚本php
:
php myscrip.php
For shebangs, here is what I use:
对于shebangs,这是我使用的:
Like this:
像这样:
#!/usr/bin/php
or this:
或这个:
#!/usr/bin/env php
回答by NVRM
Leaving here some little notes:
在这里留下一些小笔记:
To use a php binary located inside the same folder.
使用位于同一文件夹内的 php 二进制文件。
As example a php7.2 executable copied from /usr/bin
is in the same path along a hello
script.
例如,从脚本复制的 php7.2 可执行文件/usr/bin
位于同一路径中hello
。
#!./php7.2
<?php
echo "Hello!";
To run it:
运行它:
./hello
Which behave just as equal as:
其行为与以下相同:
./php7.2 hello
This give portability, but beware of system architectures, the php binary might not match the target platform.
这提供了可移植性,但要注意系统架构,php 二进制文件可能与目标平台不匹配。
Setting allowed memory from the hashbang:
从 hashbang 设置允许的内存:
We can set one INI entryfrom the hashbang line:
我们可以从 hashbang 行设置一个 INI 条目:
#!/usr/bin/php -d memory_limit=2048M
<?php
phpinfo();
exit;
Then to see if php had understood, using phpinfo():
然后使用 phpinfo() 查看 php 是否理解:
./myphpProg | grep memory
Correct shell output should contain:
正确的 shell 输出应包含:
memory_limit => 2048M => 2048M
Doing the above is similar as this command line:
执行上述操作类似于此命令行:
php -d memory_limit=2048M myphpProg.**php**
This is why we can set only one ini value in hashbangs, as php accept only one -dparameter at a time.
这就是为什么我们只能在 hashbangs 中设置一个 ini 值,因为 php 一次只接受一个-d参数。
回答by Richard A Quadling
In reply to @NVRM's comment regarding only single use of -d
, this is not true.
在回复@NVRM 关于仅使用 的评论时-d
,这是不正确的。
Start with a chmod +x script
as
以chmod +x script
as开头
#!/usr/bin/php
<?php
phpinfo();
and run script | grep -E 'memory_limit|error_reporting'
, and you'll see
然后运行script | grep -E 'memory_limit|error_reporting'
,你会看到
error_reporting => no value => no value
memory_limit => 128M => 128M
Now add some -d
entries so you have
现在添加一些-d
条目,以便您拥有
#!/usr/bin/php -d memory_limit=2G -d error_reporting=-1
<?php
phpinfo();
and re-run script | grep -E 'memory_limit|error_reporting'
, and you'll now see
并重新运行script | grep -E 'memory_limit|error_reporting'
,你现在会看到
error_reporting => -1 => -1
memory_limit => 2G => 2G
Thus demonstrating you can set multiple options.
从而证明您可以设置多个选项。
In fact, the entire command line is what you are working with here. So you can load extensions, use a different config, etc., everything you can do at the command line.
事实上,整个命令行就是您在这里使用的。所以你可以加载扩展,使用不同的配置等,你可以在命令行上做的一切。