Overview PHP command line shell_exec () using _php tutorial

Source: Internet
Author: User
Tags php cli sapi perl script
PHP after a long time of development, many users are very familiar with PHP, here I publish a personal understanding, and we discuss the PHP command line. PHP Command Line Interface (CLI) Server application Programming Interface (SAPI) was launched in PHP V4.2.0 for experimental purposes. To V4.3.0, it has been fully supported and enabled by default.

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).

 
  
  

The shell_exec () command line is actually only a variant of the anti-apostrophe (') operator. If you have written a shell or Perl script, you know that you can capture the output of other commands inside the anti-apostrophe operator. For example, listing 1 shows how to use the backslash to get the word count for each text (. txt) in the current directory.

PHP Command line overview

Listing 1. Use a backslash to calculate the number of words

 
  
  
  1. #! /bin/sh
  2. Number_of_words = ' wc-w *.txt '
  3. Echo $number _of_words
  4. #result would is something like:
  5. #165 Readme.txt 388 Results.txt 588 summary.txt
  6. #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 ()

 
  
  
  1. Php
  2. $ Results shell_exec(' wc-w *.txt ');
  3. Echo $results;
  4. ?>

Note that using the post-apostrophe operator will also get the same result, as shown below.

Listing 3. Use only the post-apostrophe operator

 
  
  
  1. Php
  2. $ Results = ' wc-w *.txt ';
  3. Echo $results;
  4. ?>

Listing 4. A simpler approach

 
  
  
  1. Php
  2. Echo ' wc-w *.txt ';
  3. ?>

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

 
  
  
  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 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.


http://www.bkjia.com/PHPjc/446471.html www.bkjia.com true http://www.bkjia.com/PHPjc/446471.html techarticle PHP After a long time of development, many users are very familiar with PHP, here I publish a personal understanding, and we discuss the PHP command line. PHP Command Line Interface (CLI) Server appli ...

  • 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.