php shell_exec() 命令用法
PHP CLI SAPI 允许您开发 PHP 支持的 shell 脚本,甚至是基于桌面的脚本。事实上,可以用PHP命令行运行的工具。采用这种方式,PHP 开发人员可以像 Perl、AWK、Ruby 或 shell 程序员一样高效。本文探究构建到 PHP 中的工具,让您了解 PHP 运行的底层 shell 环境和文件系统。PHP 为执行外部命令提供大量函数,其中包括 shell_exec()、exec()、passthru() 和 system()。这些命令是相似的,但为您运行的外部程序提供不同的界面。所有这些命令都衍生一个子进程,用于运行您指定的命令或脚本,并且每个子进程会在命令输出写到标准输出 (stdout) 时捕捉它们。
PHP CLI SAPI 允许您开发 PHP 支持的 shell 脚本,甚至是基于桌面的脚本。事实上,可以用PHP命令行运行的工具。采用这种方式,PHP 开发人员可以像 Perl、AWK、Ruby 或 shell 程序员一样高效。本文探究构建到 PHP 中的工具,让您了解 PHP 运行的底层 shell 环境和文件系统。PHP 为执行外部命令提供大量函数,其中包括 shell_exec()、exec()、passthru() 和 system()。这些命令是相似的,但为您运行的外部程序提供不同的界面。所有这些命令都衍生一个子进程,用于运行您指定的命令或脚本,并且每个子进程会在命令输出写到标准输出 (stdout) 时捕捉它们。
shell_exec()
清单 1. 使用反撇号计算单词数量
复制代码 代码如下:
#! /bin/sh
number_of_words=`wc -w *.txt`
echo $number_of_words#result would be something like:
#165 readme.txt 388 results.txt 588 summary.txt
#and so on....
在您的 PHP 脚本中,您可以在 shell_exec() 中运行这个简单的命令,如清单 2 所示,并获取想要的结果。这里假设在同一个目录下有一些文本文件。
清单 2. 在 shell_exec() 中运行相同的命令
复制代码 代码如下:
$results = shell_exec('wc -w *.txt');
echo $results;
?>
在图 1 中可以看到,获得的结果与从 shell 脚本得到的一样。这是因为 shell_exec() 允许您通过 shell 运行外部程序,然后以字符串的形式返回结果。
图 1. 通过 shell_exec() 运行 shell 命令的结果
注意,仅使用后撇号操作符也会得到相同的结果,如下所示。
清单 3. 仅使用后撇号操作符
复制代码 代码如下:
$results = `wc -w *.txt`;
echo $results;
?>
清单 4 给出了一种更加简单的方法。
清单 4. 更加简单的方法
复制代码 代码如下:
echo `wc -w *.txt`;
?>
通过 UNIX 命令行和 shell 脚本能够完成很多东西,知道这点很重要。例如,您可以使用竖线将命令连接起来。您甚至可以使用操作符在其中创建 shell 脚本,并且仅调用 shell 脚本(根据需要使用或不使用参数)。
例如,如果您仅希望计算该目录下的前 5 个文本文件的单词数,那么可以使用竖线 (|) 将 wc 和 head 命令连接起来。另外,您还可以将输出结果放到 pre 标记内部,让它能够更美观地呈现在 Web 浏览器中,如下所示。
清单 5. 更加复杂的 shell 命令
复制代码 代码如下:
$results = shell_exec('wc -w *.txt | head -5');
echo "".$results . "
";
?>
图 2 演示了运行清单 5 的脚本得到的结果。
图 2. 从 shell_exec() 运行更复杂的 shell 命令得到的结果
在本文的后面部分,您将学习如何使用 PHP 为这些脚本传递参数。现在您可以将它看作运行 shell 命令的一种方法,但要记住您只能看到标准输出。如果命令或脚本出现错误,您将看不到标准的错误 (stderr),除非您通过竖线将它添加到 stdout。
官网参考
define("__USED_CHARS__", "abcdefghijklmnopqrstuvwxyz0123456789");
define("__CASE_SENSITIVE__", true); // Use string above or use uppercase / lowercase variant$bf = new chargen(2); // new chargen object, length 2
$bf->generate("whois"); // generate chars and call whois functionfunction whois($str)
{
$domain = $str.".com";$retval = shell_exec("whois $domain");
if (eregi("no match", $retval))
echo $domain." ist availablen";
else
echo $domain." is unavailablen";
}class chargen
{
private $chars = NULL;
private $maxlength = NULL;protected $buffer = NULL;
function generate($mycallback = false)
{
foreach ($this->buffer as $char)
{
foreach ($this->chars as $nextchar)
{
$retval = $char.$nextchar;
$this->buffer[$retval] = $retval;if ($mycallback && function_exists($mycallback))
$mycallback($retval);
else
echo $retval."n";
}
}if (strlen($retval) == $this->maxlength)
return;$this->generate($mycallback);
}function __construct($maxlength = 8)
{
$chars = array();$this->buffer = array();
array_push($this->buffer, "");for ($i = 0; $i {
$index = substr(__USED_CHARS__, $i, 1);if (__CASE_SENSITIVE__)
{
$this->chars[$index] = $index;
}
else
{
$this->chars[strtolower($index)] = strtolower($index);
$this->chars[strtoupper($index)] = strtoupper($index);
}
}$this->maxlength = $maxlength;
}
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
