If you have used PHP, you will find that it is an excellent tool for creating feature-rich web pages. As a major scripting language, PHP:
·Easy to learn.
There are many powerful frameworks (such as CakePHP and CodeIgniter) that allow you to be as productive as a Rails programmer.
·Ability to communicate with MySQL, PostgreSQL, Microsoft SQL Server, and even Oracle.
·Ability to easily integrate with JavaScript frameworks such as script.aculo.us and jQuery.
But sometimes, you want to do more, or have to do more. What I mean is that you have to deal directly with the file system of the server where PHP is running. You will eventually need to work with files on the file system, learn about running processes, or perform other tasks.
First, you are satisfied with opening files in PHP using the file() command. But at some point, the only way to accomplish something is to run a shell command on the server and get a specific output. For example, you might want to know how many files a specific directory contains. Or you want to know how many lines were written to a certain set of log files. Or you want to manipulate the files, copy them to another directory, or use rsync to send them to another location.
In the article "PHP Command line? Yes, you can!" Roger McCoy demonstrates how to use PHP - No web browser required. In this post, I look at the same topic from another angle, showing you how to tightly integrate with the underlying shell commands and include return values into your interface and processes. These operations only work if you are running on Linux, Berkeley Software Distribution (BSD), or some other UNIX version. I'm assuming you're running on the Linux-Apache-MySQL-
PHP(LAMP) stack. If you are running another version of UNIX, the specific details may be different because the availability of the command line is different in each version. I know many people are still developing on Mac OS X (running some version of BSD), so I tried to keep the example commands generic to ensure easy portability.
Command line overview
PHPCommand Line Interface (CLI) Server Application Programming Interface (SAPI) was released in PHP V4.2.0 for experimental purposes. As of V4.3.0, it is fully supported and enabled by default. The PHP CLI SAPI allows you to develop PHP supported shell scripts, even desktop-based scripts. In fact, it is possible to use PHP to create tools that can be run directly from the command line. This way, PHP developers can be as productive as Perl, AWK, Ruby or shell programmers. This article explores the tools built into
PHPto 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 external programs you run. All these commands spawn a child process for running the command or script you specify, and each child process will write the command output to standard output ( stdout).
shell_exec()shell_exec() The
commandline is actually 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. Listing 1. Counting words using backticks
#!/bin/sh
Number_of_words=`wc -w *.txt`
echo $number_of_words
#result would be something like:
#165readme.txt 388results.txt 588summary.txt
#andso on....
In your
PHP script, you can run this simple command in shell_exec(), as shown in Listing 2, and get the desired results. It is assumed here that there are some text files in the same directory. Listing 2. Running the same command in shell_exec() $results =shell_exec(wc -w *.txt); echo $results; ?> $results =shell_exec(wc -w *.txt); echo $results; > As you can see in Figure 1, the results obtained are the same as those obtained from the shell script. This is because shell_exec() allows you to run an external program through the shell and then returns the result as a string. Figure 1. The result of running the shell command through shell_exec() Note that just using the trailing apostrophe operator will give you the same result, as shown below. Listing 3. Using only the trailing apostrophe operator $results =`wc -w *.txt`; echo $results; ?> $results =`wc -w *.txt`; echo $results; > Listing 4 shows a simpler approach. List 4. A simpler method echo `wc -w *.txt`; ?> echo `wc -w *.txt`; > It’s important to know that a lot can be done with 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 arguments as needed). For example, if you only want to count the number of words in the first 5 text files in the directory, you can use a vertical bar (|) 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