Dan*_*ite 336
shell_exec
将所有输出流作为字符串返回.exec
默认情况下返回输出的最后一行,但可以将所有输出提供为指定为第二个参数的数组.
看到
mpe*_*pen 73
这是差异.请注意最后的换行符.
> 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) ""
Run Code Online (Sandbox Code Playgroud)
请注意,使用反引号运算符是相同的shell_exec()
.
更新:我真的应该解释最后一个.几年后看这个答案,即使我不知道为什么会出现空白!丹尼尔在上面解释了 - 这是因为exec
只返回最后一行,而ifconfig
最后一行恰好是空白.
J0H*_*0HN 48
shell_exec
- 通过shell执行命令并将完整输出作为字符串返回
exec
- 执行外部程序.
不同之处在于,shell_exec
您将输出作为返回值.
gvi*_*iew 37
这里没有涉及的几个区别:
相比:
exec('ls', $out);
var_dump($out);
// Look an array
$out = shell_exec('ls');
var_dump($out);
// Look -- a string with newlines in it
Run Code Online (Sandbox Code Playgroud)
相反,如果命令的输出是xml或json,那么将每一行作为数组的一部分并不是你想要的,因为你需要将输入后处理成其他形式,所以在这种情况下使用shell_exec .
值得指出的是,shell_exec是fortic运算符的别名,对于那些习惯于*nix的人来说.
$out = `ls`;
var_dump($out);
Run Code Online (Sandbox Code Playgroud)
exec还支持一个附加参数,该参数将提供执行命令的返回码:
exec('ls', $out, $status);
if (0 === $status) {
var_dump($out);
} else {
echo "Command failed with status: $status";
}
Run Code Online (Sandbox Code Playgroud)
如shell_exec手册页中所述,当您实际需要从正在执行的命令返回代码时,您别无选择,只能使用exec.
归档时间: |
|
查看次数: |
414539 次 |
最近记录: |