Overview of PHP command line shell_exec() usage_PHP tutorial

WBOY
Release: 2016-07-15 13:28:10
Original
1414 people have browsed it

After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss the PHP command line with you. PHP Command Line Interface (CLI) Server Application Programming Interface (SAPI) was released starting with PHP V4.2.0 for experimental purposes. As of V4.3.0, it is fully supported and enabled by default.

PHP CLI SAPI allows you to develop PHP supported shell scripts, even desktop-based scripts. In fact, the tool can be run from the PHP command line. This way, PHP developers can be as productive as Perl, AWK, Ruby, or shell programmers. This article explores the tools built into PHP to let you understand the underlying shell environment and file system in which PHP runs. 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 the external programs you run. All of these commands spawn a child process that runs the command or script you specify, and each child process captures the command output as it is written to standard output (stdout).
<ol class="dp-xml"><li class="alt"><span><span>shell_exec() </span></span></li></ol>
Copy after login

The shell_exec() command line is really just a variation of the backtick (`) operator. If you've ever written a shell or Perl script, you know that you can capture the output of other commands inside the backtick operator. For example, Listing 1 shows how to use backticks to get the word count for each text (.txt) in the current directory.

PHP Command Line Overview

Listing 1. Counting word count using backticks

<ol class="dp-xml">
<li class="alt"><span><span>#! /bin/sh  </span></span></li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">number_of_words</font></span><span>=`wc -w *.txt`  </span>
</li>
<li class="alt"><span>echo $number_of_words  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>#result would be something like:  </span></li>
<li class=""><span>#165 readme.txt 388 results.txt 588 summary.txt  </span></li>
<li class="alt"><span>#and so on....  </span></li>
</ol>
Copy after login

In your In a PHP script, you can run this simple command in shell_exec(), as shown in Listing 2, and get the results you want. It is assumed here that there are some text files in the same directory.

Listing 2. Running the same command in shell_exec()

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>results</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>shell_exec</FONT></SPAN><SPAN>('wc -w *.txt');  </SPAN></SPAN><LI class=alt><SPAN>echo $results;  </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

Note that just using the trailing apostrophe operator will give you the same result, as follows shown.

Listing 3. Using only the trailing apostrophe operator

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>results</FONT></SPAN><SPAN> = `wc -w *.txt`;  </SPAN></SPAN><LI class=alt><SPAN>echo $results;  </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

Listing 4. A simpler approach

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>echo `wc -w *.txt`;  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

It’s important to know that a lot can be accomplished through the UNIX command line and shell scripts. For example, you can use pipes to connect commands. You can even create a shell script in it using operators and just call the shell script (with or without parameters as needed). For example, if you only want to count the words in the first 5 text files in that directory, you can use a pipe (|) to connect the wc and head commands. Alternatively, you can place the output inside a pre tag to render it more beautifully in a web browser, as shown below.

Listing 5. More complex shell commands

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>results</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>shell_exec</FONT></SPAN><SPAN>('wc -w *.txt | head -5');  </SPAN></SPAN><LI class=alt><SPAN>echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>pre</SPAN><SPAN class=tag>></span></font></strong><span>".$results . "</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>pre</SPAN><SPAN class=tag>></span></font></strong><span>";  </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

Later in this article, you will learn how to pass arguments to these scripts using PHP. Now you can think of this as a way to run a shell command, but remember that you can only see standard output. If an error occurs with a command or script, you won't see the standard error (stderr) unless you add it to stdout via a pipe.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446471.htmlTechArticleAfter a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and share it with you. Discussion discusses the PHP command line. PHP Command Line Interface (CLI) Server Appli...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template