本文翻译自: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 看到

  • http://php.net/manual/en/function.shell-exec.php http://php.net/manual/zh/function.shell-exec.php
  • http://php.net/manual/en/function.exec.php http://php.net/manual/zh/function.exec.php

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

PHP shell_exec()与exec()相关推荐

  1. php中的shell_exec函数,exec与shell_exec函数在PHP中的区别是什么

    exec与shell_exec函数在PHP中的区别是什么 发布时间:2020-12-31 17:11:47 来源:亿速云 阅读:115 作者:Leah 这期内容当中小编将会给大家带来有关exec与sh ...

  2. php shell exec 阻塞,php shell_exec()vs exec()

    我正在努力理解shell_exec()和exec()之间的区别- 我总是使用exec()来执行服务器端命令,何时使用shell_exec()? shell_exec()只是 exec()`的缩写.这似 ...

  3. 导致命令注入漏洞的php函数,PHP安全-函数

    函数 在我写作本书的时候,http://www.gxlcms.com/列出了共3917个函数,其中包括一些类似函数的语法结构,在此我不准备把它们从函数中区分开来,而是把它作为函数看待. 由于函数数量很 ...

  4. 如何关闭PHP的安全模式,Linux下的Apache和PHP安全设置,如何开启PHP的安全模式

    Linux下的Apache和PHP安全设置 PHP安全模式开启,PHP5.3将不再有安全模式. (1) safe_mode:以安全模式运行php; 在php.ini文件中使用如下 safe_mode ...

  5. Web后门工具WeBaCoo

    Web后门工具WeBaCoo WeBaCoo是使用Perl语言编写的Web后门工具.渗透测试人员首先使用该工具生成一个后门PHP页面.然后,将该页面上传到目标服务器上.最后,在本地终端直接访问该页面, ...

  6. PHP 安全问题入门:10 个常见安全问题 + 实例讲解

    文章转自:learnku.com/php/t/24930 更多文章:learnku.com/laravel/c/t- 相对于其他几种语言来说, PHP 在 web 建站方面有更大的优势,即使是新手,也 ...

  7. php 开启imagick,PHP-Imagick:在Imagick项目上设置重力

    在Imagick中设置图像的重力时,我遇到了一些实际困难. 我已经成功设置了ImaickDraw对象的重力,但是我没有成功在Imagick对象中设置它. 以下是此刻我正在使用的基本代码.我只是使用了与 ...

  8. [导入]php 安全基础 附录B. 函数

    附录B. 函数 在我写作本书的时候,http://php.net/quickref.php列出了共3917个函数,其中包括一些类似函数的语法结构,在此我不准备把它们从函数中区分开来,而是把它作为函数看 ...

  9. 在gitlab 中使用webhook 实现php 自动部署git 代码

    在技术团队讨论中,我们决定从svn 迁移到 git ,于是使用了gitlab,代码自动部署使用了webhook 在服务器上 1.开启PHP需要的环境支持 服务器环境必须先安装git 环境,webhoo ...

最新文章

  1. docker 使用python 镜像运行python脚本
  2. iOS内存管理知识点
  3. Java中swing和awt初了解
  4. 自律到极致-人生才精致:第11期
  5. Java JNI简单实现
  6. 做任务一定要看测试用例
  7. IntelliJ IDEA中快捷键大全+出现的问题
  8. WildFly 8.2.0.Final版本–更改的快速概述
  9. GitHub注册和Git安装
  10. Linux start-kernel
  11. 年纪轻轻却突然猝死?数据分析告诉你“猝死”离我们到底有多近?
  12. LC415字符串相加
  13. [转载]Java程序占用 CPU 过高怎么排查
  14. 迅捷路由器造成计算机无法上网,迅捷(fast)路由器连不上网怎么办?
  15. python画k线_python下画k线
  16. 穷建站(二):在DnsPod中为申请的tk域名进行域名解析
  17. 微信小程序如何做触底加载分页功能
  18. 03-KVM虚拟磁盘格式及快照
  19. bind peeking--绑定变量窥视
  20. 【缓存中间件】redis 支持的数据类型

热门文章

  1. observable.unsubscribeOn(Schedulers.io())
  2. 用神经集认识手写数字
  3. 自己动手写C语言编译器(5)
  4. CUSTOMDRAW msdn网站
  5. 滴滴哆啦A梦源码解析
  6. 第九周项目三-人数不定的工资类
  7. 如何让项目一步步走向灭亡
  8. 创建型模式--工厂方法模式
  9. (0053)iOS开发之沙盒(sandbox)机制和文件操作(三)
  10. swift_026(Swift 的类型转换)