Home Backend Development PHP Tutorial Overview of PHP command line shell_exec() usage_PHP tutorial

Overview of PHP command line shell_exec() usage_PHP tutorial

Jul 15, 2016 pm 01:28 PM
exec php shell learn use develop Command Line Multi-user I Overview

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles