As we all know, PHP is a very good dynamic web development language (fast, short development cycle ...). )。 But only a very small number of people realize that PHP can also be very good as the language of writing shell scripts, when PHP as the language of writing shell scripts, he is not as powerful as Perl or bash, but he has a good advantage, Especially for those of me who are familiar with PHP but are not familiar with Perl.
To use PHP as the Shell scripting language, you have to compile PHP as a binary CGI instead of Apache, and there are some security issues with PHP compiled into binary CGI mode, which can be found in the PHP Manual (http://www.php.net).
You might not get used to writing shell scripts at first, but it will get better: the only difference between PHP as a generic Dynamic Web page and as a Shell scripting language is that a shell script needs to explain the script's program path in the first line of life:
We add the parameter "1" After the PHP execution file, so that the child PHP will not output httpheader (if you still need to be a Dynamic Web page, then you need to use the header function output Httpheader). Of course, in the shell script you still need to use PHP's start and end tags:
Now let's look at an example to better understand the use of PHP as a shell scripting language:
Copy the Code code as follows:
Print ("Hello, world!n");
?>
The above program will simply output "Hello, world!" to the monitor.
First, pass the shell script run parameters to PHP:
As a shell script, often add some parameters when running the program, PHP as a shell script with an inline array "$argv", using the "$ARGV" array can easily read the shell script Runtime parameters ("$ARGV [1]" Corresponds to the first argument, "$ARGV [2]" corresponds to the second parameter, and so on. For example, the following program:
Copy the Code code as follows:
#!/usr/local/bin/php-q
$first _name = $argv [1];
$last _name = $argv [2];
printf ("Hello,%s%s! How is You Today?n ", $first _name, $last _name);
?>
The above code requires two parameters at run time, namely, last name and first name, such as run:
[Dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon
The shell script will output on the display:
Hello, Darrell brogdon! How is you today?
[Dbrogdon@artemis dbrogdon]$
PHP also contains the "$ARGV" array when it is written as a dynamic Web page, but there are some differences: when PHP is the Shell scripting language, "$argv [0]" corresponds to the file name of the script, and when used for dynamic Web page writing, "$argv [1]" Corresponds to the first parameter of the QueryString.
Second, write an interactive shell script:
If a shell script just runs on its own and loses interactivity, then it doesn't mean anything. When PHP is used for shell script writing, how to read the user input information? Unfortunately, PHP itself does not have a function or method to read user input information, but we can follow other languages to write a function that reads user input information "read":
Copy the Code code as follows:
function Read () {
$fp = fopen ('/dev/stdin ', ' R ');
$input = fgets ($fp, 255);
Fclose ($FP);
return $input;
}
?>
It is important to note that the above function can only be used for UNIX systems (other systems need to be changed accordingly). The above function opens a file pointer and then reads a line of no more than 255 bytes (that is, fgets), then closes the file pointer and returns the read information.
Now we can use the function "read" To modify the program we wrote earlier in 1 to make him more "interactive":
Copy the Code code as follows:
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's your last name?");
$last _name = read ();
Print ("Nhello, $first _name $last _name! Nice to meet You!n ");
?>
Save the above program and run it, you may see something unexpected: The last line of input becomes three lines! This is because the information returned by the "read" function also includes the end-of-line newline character "\ n" for each row of the user, which is retained in the first and last names, and the "read" function needs to be modified by removing the end of the line break:
Copy the Code code as follows:
function Read () {
$fp = fopen ('/dev/stdin ', ' R ');
$input = fgets ($fp, 255);
Fclose ($FP);
$input = Chop ($input); Remove trailing blanks
return $input;
}
?>
C. Shell scripts written in other languages include shell scripts written by PHP:
Sometimes we may need to include shell scripts written by PHP in shell scripts written in other languages. Actually very simple, here is a simple example:
Echo this was the Bash section of the code.
Copy the Code code as follows:
/usr/local/bin/php-q << EOF
Print ("This was the PHP section of the Coden");
?>
Eof
In fact, it is called PHP to parse the following code, and then output; then try the following code:
Echo this was the Bash section of the code.
Copy the Code code as follows:
/usr/local/bin/php-q << EOF
$myVar = ' PHP ';
Print ("This was the $myVar section of the Coden");
?>
Eof
You can see that the only difference in code two times is the second time using a variable "$myVar", try running, PHP actually gave the error message: "Parse error:parse error in-on line 2"! This is because the variables in bash are also "$myVar", and the bash parser replaces the variables first, and to solve this problem, you need to precede each PHP variable with a "\" escape character, then the code is modified as follows:
Echo this was the Bash section of the code.
Copy the Code code as follows:
/usr/local/bin/php-q << EOF
\ $myVar = ' PHP ';
Print ("This was the \ $myVar section of the Coden");
?>
Eof
Well, now you can write your own shell script in PHP, and I hope everything goes well with you. If there is any problem, you can go to the forum to discuss the site.
English version address: http://www.phpbuilder.com/columns/darrell20000319.php3
http://www.bkjia.com/PHPjc/314942.html www.bkjia.com true http://www.bkjia.com/PHPjc/314942.html techarticle As we all know, PHP is a very good dynamic web development language (fast, short development cycle ...). )。 But only a very small number of people realize that PHP can also be well written as ...