We all know that php (as the mainstream development language) is a very good dynamic web page development language (fast speed, short development cycle ......). However, only a few people realize that php (as the current mainstream development language) can also be a good language for writing Shell scripts. When php (as the current mainstream development language) SyntaxHighlighter. al
We all know that php (as the mainstream development language) is a very good dynamic web page development language (fast speed, short development cycle ......). However, only a few people realize that php (as the mainstream development language) can also be a good language for writing Shell scripts. When php (as the mainstream development language) as a language for writing Shell scripts, he is not as powerful as Perl or Bash, but he has good advantages, especially for my familiarity with php (as the mainstream development language) but not familiar with Perl.
To use php (as the mainstream development language) as the Shell scripting language, you must use php (as the mainstream development language) as a binary cgi (not many people are using it now) compilation, rather than apache (the most popular WEB server platform on the Unix platform) mode; compiling php that runs in the binary cgi mode (not many people are using it now) has some security issues, for the solution, see The php (as the mainstream development language) manual (http://www.php (as the mainstream development language ).. net ).
At the beginning, you may not be able to write Shell scripts, but it will be better: use php as the mainstream development language) the only difference between dynamic web writing language and Shell scripting language is that a Shell script needs to explain the program path of this script in the first line of life:
We have added the parameter "-1" to the execution file of php (as the mainstream development language), so php (as the mainstream development language) HTTPHeader is not output (if you still need to serve as a dynamic Web page of the Web, you need to use the header function to output HTTPHeader ). Of course, in Shell scripts, you still need to use the starting and ending labels of php (as the mainstream development language:
Now let's look at an example to better understand the use of php (as the mainstream development language) as the 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 the mainstream development language ): As a Shell script, some parameters are often added when running the program. php (as the mainstream development language), as a Shell script, 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 (as the mainstream development language)-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 (as the mainstream development language) is used as the dynamic web page programming language, it also contains the array "$ argv", but it is somewhat different from here: when php (as the mainstream development language) 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 (as the mainstream development language) is used for Shell script writing? Unfortunately, php (as the mainstream development language) does not read user input functions or methods, however, 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 ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.