PHP shell_exec () command usage _php tutorial

Source: Internet
Author: User
Tags php cli sapi
PHP CLI SAPI allows you to develop PHP-supported shell scripts, even desktop-based scripts. In fact, you can run the tool with the PHP command line. In this way, PHP developers can be as efficient as Perl, AWK, Ruby, or shell programmers. This article explores the tools built into PHP to give you an idea of the underlying shell environment and file system that PHP is running. PHP provides a number of functions for executing external commands, including shell_exec (), exec (), PassThru (), and System (). These commands are similar, but provide different interfaces for external programs that you run. All of these commands derive a subprocess that runs the commands or scripts that you specify, and each child process captures them when the command output is written to standard output (stdout).

PHP CLI SAPI allows you to develop PHP-supported shell scripts, even desktop-based scripts. In fact, you can run the tool with the PHP command line. In this way, PHP developers can be as efficient as Perl, AWK, Ruby, or shell programmers. This article explores the tools built into PHP to give you an idea of the underlying shell environment and file system that PHP is running. PHP provides a number of functions for executing external commands, including shell_exec (), exec (), PassThru (), and System (). These commands are similar, but provide different interfaces for external programs that you run. All of these commands derive a subprocess that runs the commands or scripts that you specify, and each child process captures them when the command output is written to standard output (stdout).

Shell_exec ()

Listing 1. Use a backslash to calculate the number of words
Copy the code code as follows:

#! /bin/sh
number_of_words= ' wc-w *.txt '
Echo $number _of_words

#result would is something like:
#165 Readme.txt 388 Results.txt 588 summary.txt
#and so on ....

In your PHP script, you can run this simple command in Shell_exec (), as shown in Listing 2, and get the results you want. This assumes that there are some text files in the same directory.

Listing 2. Run the same command in Shell_exec ()
Copy the code code as follows:

$results = shell_exec (' wc-w *.txt ');
Echo $results;
?>

As you can see in Figure 1, the results are the same as those obtained from the shell script. This is because Shell_exec () allows you to run external programs through the shell and then return the results as strings.
Figure 1. The result of running the shell command through Shell_exec ()

Note that using the post-apostrophe operator will also get the same result, as shown below.
Listing 3. Use only the post-apostrophe operator
Copy the code code as follows:

$results = ' wc-w *.txt ';
Echo $results;
?>

Listing 4 shows a simpler approach.
Listing 4. A simpler approach
Copy the code code as follows:

Echo ' wc-w *.txt ';
?>

It is important to know that there are many things that can be done with UNIX command line and shell scripts. For example, you can use a vertical bar to connect commands. You can even use the operator to create a shell script in it, and call only the shell script (using or not using parameters as needed).

For example, if you only want to calculate the number of words for the first 5 text files in that directory, you can use a vertical bar (|) to connect the WC and head commands. In addition, you can put the output inside the pre tag so that it can be rendered more aesthetically in a Web browser, as shown below.

Listing 5. More complex shell commands
Copy the code code as follows:

$results = shell_exec (' wc-w *.txt | head-5 ');
echo " ".$results . " ";
?>

Figure 2 illustrates the result of running the script in Listing 5.
Figure 2. Results from running more complex shell commands from shell_exec ()

Later in this article, you will learn how to use PHP to pass parameters to these scripts. You can now see it as a way to run a shell command, but remember that you only have the standard output. If there is an error in the command or script, you will not see the standard error (STDERR) unless you add it to stdout through a vertical bar.

Website reference

Define ("__used_chars__", "abcdefghijklmnopqrstuvwxyz0123456789");
Define ("__case_sensitive__", true); Use a string above or use Uppercase/lowercase variant

$BF = new Chargen (2); New Chargen object, length 2
$BF->generate ("whois"); Generate chars and call whois function

function 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 < strlen (__used_chars__); $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;
}
}
?>


http://www.bkjia.com/PHPjc/444752.html www.bkjia.com true http://www.bkjia.com/PHPjc/444752.html techarticle PHP CLI SAPI allows you to develop PHP-supported shell scripts, even desktop-based scripts. In fact, you can run the tool with the PHP command line. In this way, PHP developers can ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.