
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");

打印输出(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);

将返回值分配给变量(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;

使用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');

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命令,例如date
, whoami
, ifconfig
和mkdir
。
> echo shell_exec('date');
> echo shell_exec('whoami');
> echo shell_exec('ifconfig');

使用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()
函数直接显示输出,而无需使用echo
或print
。 在此示例中,我们将再次运行ls
命令。
system('ls');

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;

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