Overview of PHP command line shell_exec() usage_PHP tutorial
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.
<ol class="dp-xml"><li class="alt"><span><span>shell_exec() </span></span></li></ol>
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>
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>
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>
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>
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>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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

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