Overview use of PHP Command Line shell_exec ()

Source: Internet
Author: User
Tags call shell php cli sapi

PHP has been developing for a long time and many users are familiar with PHP. Here I will share my personal understanding and discuss with you the PHP Command Line. PHP Command Line Interface (CLI) Server Application Programming Interface (SAPI) is released in PHP V4.2.0 for testing purposes. To V4.3.0, it is fully supported and enabled by default.

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.

 
 
  1. shell_exec() 

The shell_exec () command line is actually only a variant of the anti-apostrophes (') operator. If you have written shell or Perl scripts, you can capture the output of other commands in the anti-float operator. For example, listing 1 shows how to use the extension number to count the words in each vertex (.txt) in the current directory.

PHP Command Line Overview

List 1. Calculate the number of words using the anti-marker

 
 
  1. #! /bin/sh  
  2. number_of_words=`wc -w *.txt`  
  3. echo $number_of_words  
  4.  
  5. #result would be something like:  
  6. #165 readme.txt 388 results.txt 588 summary.txt  
  7. #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 ()

 
 
  1. <?php 
  2. $results = shell_exec('wc -w *.txt');  
  3. echo $results;  
  4. ?> 

Note that only the suffix marker operator will get the same result, as shown below.

Listing 3. Only use the suffix marker Operator

 
 
  1. <?php 
  2. $results = `wc -w *.txt`;  
  3. echo $results;  
  4. ?> 

Listing 4. simpler methods

 
 
  1. <?php 
  2. echo `wc -w *.txt`;  
  3. ?> 

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

 
 
  1. <?php 
  2. $results = shell_exec('wc -w *.txt | head -5');  
  3. echo "<pre>".$results . "</pre>";  
  4. ?> 

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.


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.