shell脚本执行exec_如何使用PHP Exec和示例执行Shell命令

本文介绍了PHP中的exec()和shell_exec()函数,用于执行系统命令。exec()函数允许接收命令输出,并能捕获返回值,而shell_exec()则返回命令的完整输出字符串。文章通过示例展示了如何创建目录、列出文件、获取命令返回值等操作,同时对比了这两个函数的使用差异。
摘要由CSDN通过智能技术生成
shell脚本执行exec

shell脚本执行exec

Php provides web-based functionalities to develop web applications. But it also provides system related scripting and execution features. exec() function is used to execute an external binary or program from a PHP script or application. In this tutorial, we will look at different use cases and examples of exec() function like return value, stderr, shell_exec, etc.

Php提供基于Web的功能来开发Web应用程序。 但它也提供与系统相关的脚本和执行功能。 exec()函数用于从PHP脚本或应用程序执行外部二进制文件或程序。 在本教程中,我们将研究不同的用例和exec()函数的示例,例如返回值,stderr,shell_exec等。

exec()函数语法 (exec() Function Syntax)

The PHP exec()syntax is like below where single parameter is mandatory and other parameters are optional.

PHP exec()语法如下所示,其中单个参数是必需的,其他参数是可选的。

exec(string COMMAND, array OUTPUT, int RETURN_VARIABLE);

COMMAND is the command we want to execute with exec() function. The command should be a string value or variable. COMMAND is a mandatory parameter.

COMMAND是我们要通过exec()函数执行的命令。 该命令应为字符串值或变量。 COMMAND是必填参数。

OUTPUT is the output of the COMMAND execution. OUTPUT is an array that can hold multiple values or lines. OUTPUT is optional where it can be omitted.

OUTPUT是COMMAND执行的输出。 OUTPUT是一个可以容纳多个值或多行的数组。 OUTPUT是可选的,可以省略。

RETURN_VARIABLE is the return value of the given COMMAND. RETURN _VALUE is generally the process status of the command. RETURN_VALUE is an integer and optional to use.

RETURN_VARIABLE是给定COMMAND的返回值。 RETURN _VALUE通常是命令的进程状态。 RETURN_VALUE是一个整数,可以选择使用。

运行外部系统二进制文件或应用程序 (Run External System Binary or Application)

We will start with a simple example. We will provide the command we want to run on the local operating system. In this example, we will create a directory named data. This directory will be created in the current working path. We can also specify the path explicitly like /var/data.

我们将从一个简单的例子开始。 我们将提供要在本地操作系统上运行的命令。 在此示例中,我们将创建一个名为data的目录。 该目录将在当前工作路径中创建。 我们还可以像/var/data一样显式指定路径。

 exec("mkdir data");

We can list the created directory with Linux file command like below. We will also provide the directory name because exec() function will only show the last line of the output. We will use echo to print the command output.

我们可以使用Linux file命令列出创建的目录,如下所示。 我们还将提供目录名称,因为exec()函数将仅显示输出的最后一行。 我们将使用echo打印命令输出。

echo exec("file data");
Run External System Binary or Application
Run External System Binary or Application
运行外部系统二进制文件或应用程序

打印输出(Print Output)

We have already looked at printing output but I want to explain more about printing output. exec() function output or return can be printed with echo but the printed part will be only last line. So in a multi-line output, we can not see the output with echo. If we want to see the whole output we should use the system() function which will be explained below.

我们已经看过打印输出,但是我想解释更多有关打印输出的信息。 exec()函数的输出或返回可以用echo打印,但打印的部分将仅是最后一行。 因此,在多行输出中,我们看不到带有echo的输出。 如果要查看整个输出,则应使用system()函数,下面将对其进行说明。

But we can use the output parameter like below. In this example, we will put command output to the o. The output parameter is in array type so we will use print_r to print output.

但是我们可以使用如下所示的output参数。 在此示例中,我们将命令输出放到o 。 输出参数为数组类型,因此我们将使用print_r打印输出。

exec("ls",$o);
print_r($o);
PHP exec Output
PHP exec输出

将返回值分配给变量(Assign Return Value into Variable)

Using echo is not a reliable way to get the return value. We can use variables to set return value and use whatever we want. Int this example we will set the process return value to v variable.

使用echo不是获取返回值的可靠方法。 我们可以使用变量来设置返回值并使用我们想要的任何东西。 在此示例中,我们将流程返回值设置为v变量。

exec("ls",$o,$v);
echo $v;
Assign Return Value into Variable
Assign Return Value into Variable
将返回值分配给变量

使用shell_exec()以String形式返回完整输出(Return Complete Output as String with shell_exec())

PHP language provides different functions as an alternative to exec(). We can use the shell_exec() function which will create a shell process and run given command. In this example, we will look at ls command and print output. With the shell_exec() function we can not get the return value of the shell process or command.

PHP语言提供了不同的功能来替代exec() 。 我们可以使用shell_exec()函数来创建一个shell进程并运行给定的命令。 在此示例中,我们将查看ls命令并打印输出。 使用shell_exec()函数,我们无法获取shell进程或命令的返回值。

echo shell_exec('ls');
Return Complete Output as String with shell_exec()
Return Complete Output as String with shell_exec()
使用shell_exec()以String形式返回完整输出

PHP shell_exec()示例(PHP shell_exec() Examples)

In this part, we will make more examples about the PHP shell_exec() function. We will run different system and Linux commands like date , whoami , ifconfig and mkdir .

在这一部分中,我们将提供有关PHP shell_exec()函数的更多示例。 我们将运行不同的系统和Linux命令,例如datewhoamiifconfigmkdir

> echo shell_exec('date');

> echo shell_exec('whoami');

> echo shell_exec('ifconfig');
PHP shell_exec() Examples
PHP shell_exec()示例

使用system()以String形式返回完整输出(Return Complete Output as String with system())

Another similar function is system(). system() function displays output directly without using echo or print. In this example, we will run the ls command again.

另一个类似的函数是system()system()函数直接显示输出,而无需使用echoprint 。 在此示例中,我们将再次运行ls命令。

 system('ls');
Return Complete Output as String with system()
Return Complete Output as String with system()
使用system()以String形式返回完整输出

exec()函数与shell_exec()函数(exec() Function vs shell_exec() Function)

PHP also provides the shell_exec() function which is very similar to the exec() function. The main difference is shell_exec() function accept single parameter which is command and returns the output as string.

PHP还提供了shell_exec()函数,该函数与exec()函数非常相似。 主要区别在于shell_exec()函数接受单个参数,该参数为command并以字符串形式返回输出。

//Execude command in the shell with PHP exec() function
//put the output to the $output variable 
exec("uname -a",$output,$return_val);

print_r($output);

//Execude command in the shell with PHP shell_exec() function
//put the output into $out variable
$out = shell_exec("uname -a");

echo $out;
exec() Function vs shel_exec() Function
exec() Function vs shel_exec() Function
exec()函数与shel_exec()函数
LEARN MORE  Exec System Call For C and PHP Tutorial with Examples
了解更多有关C和PHP教程的Exec系统调用示例

翻译自: https://www.poftut.com/execute-shell-commands-php-exec-examples/

shell脚本执行exec

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值