We all know that PHP is a very good dynamic web page development language (fast speed, short development cycle ......). However, only a few people realize that PHP can also be a good language for writing Shell scripts. When PHP is used as a language for writing Shell scripts, it is not as powerful as Perl or Bash, but he has a good advantage, especially for people who are familiar with PHP but are not familiar with Perl. We all know that PHP is a very good dynamic web development language (fast speed, short development cycle...) to use PHP as a Shell scripting language ......). However, only a few people realize that PHP can also be a good language for writing Shell scripts. When PHP is used as a language for writing Shell scripts, it is not as powerful as Perl or Bash, but he has a good advantage, especially for people who are familiar with PHP but are not familiar with Perl.
To use PHP as the Shell scripting language, you must compile PHP as a binary CGI instead of apache (the most popular WEB server platform on the Unix platform; compiling PHP to run in binary CGI mode has some security issues. for the solution, see The PHP Manual (http://www.php.net ).
At first, you may feel uncomfortable with writing Shell scripts, but it will be better: the only difference between using PHP as a general dynamic web writing language and using Shell scripting language is that a Shell script needs to explain the program path of this script in the first line of life:
We add the parameter "-1" after the PHP execution file, so that PHP will not output HTTPHeader (if it still needs to be used as a dynamic Web page of the Web, then you need to use the header function to output HTTPHeader ). Of course, you still need to use the PHP start and end mark in the Shell script:
Now let's look at an example to better understand the use of PHP as a Shell scripting language: Print ("Hello, world! N ");
?>
The above program will simply output "Hello, world !" To the Monitor.
1. pass the Shell script running parameters to PHP:
As a Shell script, some parameters are often added when the program is running. as a Shell script, PHP has an embedded array "$ argv ", using the "$ argv" array, you can easily read the parameters during Shell script running ("$ argv [1]" corresponds to the first parameter, "$ argv [2]" corresponds to the second parameter, and so on ). For example, the following program :#! /Usr/local/bin/php-q
$ First_name = $ argv [1];
$ Last_name = $ argv [2];
Printf ("Hello, % s! How are you today? N ", $ first_name, $ last_name );
?>
The above code requires two parameters, namely, the last name and the first name, for example:
[Dbrogdon @ artemis dbrogdon] $ scriptname. ph Darrell Brogdon
The Shell script will output on the display:
Hello, Darrell Brogdon! How are you today?
[Dbrogdon @ artemis dbrogdon] $
When PHP is used as a dynamic web page programming language, it also contains the array "$ argv", but there are some differences with this: when PHP is used as the Shell scripting language, "$ argv [0]" corresponds to the script file name. when it is used for dynamic web page writing, "$ argv [1]" corresponds to the first parameter of QueryString.
2. Compile an interactive Shell script:
If a Shell script only runs on its own and loses its interactivity, it doesn't mean anything. How can I read user input information when PHP is used to write Shell scripts? Unfortunately, PHP itself does not have a function or method to read user input information, but we can compile a function "read" to read user input information in other languages ": Function read (){
$ Fp = fopen ('/dev/stdin', 'r ');
$ Input = fgets ($ fp, 255 );
Fclose ($ fp );
Return $ input;
}
?>
Note that the above function can only be used in Unix systems (other systems need to be changed accordingly ). The above function will open a file pointer, then read a row of no more than 255 bytes (that is, the function of fgets), and then close the file pointer to return the read information.
Now we can use the function "read" to modify the program 1 we have previously compiled to make it more "interactive: Function read (){
$ Fp = fopen ('/dev/stdin', 'r ');
$ Input = fgets ($ fp, 255 );
Fclose ($ fp );
Return $ input;
}
Print ("What is your first name? ");
$ First_name = read ();
Print ("What is your last name? ");
$ Last_name = read ();