PHP shell_exec()与exec()

本文翻译自:PHP shell_exec() vs exec()

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() ? shell_exec()只是exec()的简写吗? It seems to be the same thing with fewer parameters. 较少的参数似乎是同一件事。


#1楼

参考:https://stackoom.com/question/TlR6/PHP-shell-exec-与exec


#2楼

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 operator is 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. Daniel在上面进行了解释-这是因为exec仅返回最后一行,而ifconfig的最后一行碰巧是空白。


#3楼

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. 使用exec(),您可以传递一个可选的param变量,该变量将接收输出行数组。 In some cases this might save time, especially if the output of the commands is already tabular. 在某些情况下,这可能节省时间,尤其是在命令输出已经以表格形式显示的情况下。

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. 还值得指出的是,shell_exec是backtic运算符的别名,对于* nix而言是这样的。

$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。


#4楼

shell_exec returns all of the output stream as a string. shell_exec以字符串形式返回所有输出流。 exec returns the last line of the output by default, but can provide all output as an array specifed as the second parameter. exec默认情况下返回输出的最后一行,但是可以将所有输出作为指定为第二个参数的数组提供。

See 看到


#5楼

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可以将输出作为返回值。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值