PHP shell_exec() 与 exec()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7093860/
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
PHP shell_exec() vs exec()
提问by Webnet
I'm struggling to understand the difference between shell_exec()
and exec()
...
我正在努力理解shell_exec()
和exec()
...之间的区别
I've always used exec()
to execute server side commands, when would I use shell_exec()
?
我一直用来exec()
执行服务器端命令,我shell_exec()
什么时候使用?
Is shell_exec()
just a shorthand for exec()
? It seems to be the same thing with fewer parameters.
是否shell_exec()
只是一个速记exec()
?参数较少的情况似乎是一样的。
回答by Daniel A. White
shell_exec
returns all of the output stream as a string. exec
returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.
shell_exec
以字符串形式返回所有输出流。exec
默认情况下返回输出的最后一行,但可以将所有输出作为指定为第二个参数的数组提供。
See
看
回答by mpen
Here are the differences. Note the newlines at the end.
以下是差异。注意末尾的换行符。
> shell_exec('date')
string(29) "Wed Mar 6 14:18:08 PST 2013\n"
> exec('date')
string(28) "Wed Mar 6 14:18:12 PST 2013"
> shell_exec('whoami')
string(9) "mark\n"
> exec('whoami')
string(8) "mark"
> shell_exec('ifconfig')
string(1244) "eth0 Link encap:Ethernet HWaddr 10:bf:44:44:22:33 \n inet addr:192.168.0.90 Bcast:192.168.0.255 Mask:255.255.255.0\n inet6 addr: fe80::12bf:ffff:eeee:2222/64 Scope:Link\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n RX packets:16264200 errors:0 dropped:1 overruns:0 frame:0\n TX packets:7205647 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000 \n RX bytes:13151177627 (13.1 GB) TX bytes:2779457335 (2.7 GB)\n"...
> exec('ifconfig')
string(0) ""
Note that use of the backtick operatoris identical to shell_exec()
.
请注意,反引号运算符的使用与shell_exec()
.
Update:I really should explain that last one. Looking at this answer years later even I don't know why that came out blank! Daniel explains it above -- it's because exec
only returns the last line, and ifconfig
's last line happens to be blank.
更新:我真的应该解释最后一个。多年后看这个答案,我也不知道为什么结果是空白的!丹尼尔在上面解释过——这是因为exec
只返回最后一行,而ifconfig
的最后一行恰好是空白的。
回答by J0HN
shell_exec
- Execute command via shell and return the complete output as a string
shell_exec
- 通过 shell 执行命令并以字符串形式返回完整的输出
exec
- Execute an external program.
exec
- 执行外部程序。
The difference is that with shell_exec
you get output as a return value.
不同之处在于shell_exec
您将输出作为返回值。
回答by gview
A couple of distinctions that weren't touched on here:
这里没有涉及的几个区别:
- With exec(), you can pass an optional param variable which will receive an array of output lines. In some cases this might save time, especially if the output of the commands is already tabular.
- 使用 exec(),您可以传递一个可选的 param 变量,该变量将接收一组输出行。在某些情况下,这可能会节省时间,尤其是在命令的输出已经是表格的情况下。
Compare:
相比:
exec('ls', $out);
var_dump($out);
// Look an array
$out = shell_exec('ls');
var_dump($out);
// Look -- a string with newlines in it
Conversely, if the output of the command is xml or json, then having each line as part of an array is not what you want, as you'll need to post-process the input into some other form, so in that case use shell_exec.
相反,如果命令的输出是 xml 或 json,那么将每一行作为数组的一部分并不是您想要的,因为您需要将输入后处理为其他形式,因此在这种情况下使用 shell_exec .
It's also worth pointing out that shell_exec is an alias for the backtic operator, for those used to *nix.
还值得指出的是,对于那些习惯使用 *nix 的人来说,shell_exec 是 backtic 运算符的别名。
$out = `ls`;
var_dump($out);
exec also supports an additional parameter that will provide the return code from the executed command:
exec 还支持一个附加参数,该参数将提供已执行命令的返回码:
exec('ls', $out, $status);
if (0 === $status) {
var_dump($out);
} else {
echo "Command failed with status: $status";
}
As noted in the shell_exec manual page, when you actually require a return code from the command being executed, you have no choice but to use exec.
如 shell_exec 手册页所述,当您实际上需要正在执行的命令的返回码时,您别无选择,只能使用 exec。