Phpshell_exec () command usage. PHPCLISAPI allows you to develop shell scripts supported by PHP, or even desktop-based scripts. In fact, you can use PHP command line to run the tool. In this way, PHP developers can use php cli sapi to allow you to develop shell scripts supported by PHP, or even desktop-based scripts. In fact, you can use PHP command line to run the tool. In this way, PHP developers can be as efficient as Perl, AWK, Ruby, or shell programmers. This article explores the tools built into PHP, allowing you to understand the underlying shell environment and file system running PHP. PHP provides a large number of functions for executing external commands, including shell_exec (), exec (), passthru (), and system (). These commands are similar, but provide different interfaces for the external programs you run. All these commands are derived from a sub-process used to run your specified commands or scripts, and each sub-process will capture them when the command output is written to the standard output (stdout.
Php cli sapi allows you to develop shell scripts supported by PHP, or even desktop-based scripts. In fact, you can use PHP command line to run the tool. In this way, PHP developers can be as efficient as Perl, AWK, Ruby, or shell programmers. This article explores the tools built into PHP, allowing you to understand the underlying shell environment and file system running PHP. PHP provides a large number of functions for executing external commands, including shell_exec (), exec (), passthru (), and system (). These commands are similar, but provide different interfaces for the external programs you run. All these commands are derived from a sub-process used to run your specified commands or scripts, and each sub-process will capture them when the command output is written to the standard output (stdout.
Shell_exec ()
List 1. calculate the number of words using the anti-marker
The code is as follows:
#! /Bin/sh
Number_of_words = 'WC-w *. txt'
Echo $ number_of_words
# Result wocould be 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 desired result. Suppose there are some text files in the same directory.
Listing 2. run the same command in shell_exec ()
The code is as follows:
$ Results = shell_exec ('WC-w *. txt ');
Echo $ results;
?>
As shown 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 shell and then return results in the form of strings.
Figure 1. result of running shell command through shell_exec ()
Note that only the suffix marker operator will get the same result, as shown below.
Listing 3. only use the suffix marker operator
The code is as follows:
$ Results = 'WC-w *. txt ';
Echo $ results;
?>
Listing 4 provides a simpler method.
Listing 4. simpler methods
The code is as follows:
Echo 'WC-w *. txt ';
?>
It is important to know that many things can be done through UNIX command lines and shell scripts. For example, you can use a vertical line to connect commands. You can even use operators to create shell scripts in them and only call shell scripts (use or not use parameters as needed ).
For example, if you only want to calculate the number of words in the first five text files in the directory, you can use a vertical line (|) to connect the wc and head commands. In addition, you can place the output result in the pre tag so that it can be visually displayed in a Web browser, as shown below.
Listing 5. more complex shell commands
The code is as follows:
$ Results = shell_exec ('WC-w *. txt | head-5 ');
Echo" ".$results . "";
?>
Figure 2 demonstrates the result of running the script in listing 5.
Figure 2. result of running more complex shell commands from shell_exec ()
Later in this article, you will learn how to use PHP to pass parameters for these scripts. Now you can think of it as a way to run shell commands, but remember that you can only see standard output. If a command or script has an error, you will not see the standard error (stderr) unless you add it to stdout through a vertical line.
Official website reference
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 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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444752.htmlTechArticlePHP cli sapi allows you to develop shell scripts supported by PHP, or even desktop-based scripts. In fact, you can use PHP command line to run the tool. In this way, PHP developers can...