We all know that php (as the current mainstream development language) is a very good dynamic web development language (fast speed, short development cycle...). But only a few people realize that php (as the current mainstream development language) can also be used as a good language for writing Shell scripts. When php (as the current mainstream development language) )As a language for writing Shell scripts, it is not as powerful as Perl or Bash, but it has good advantages, especially for people like me who are familiar with php (as the current mainstream development language) But people who are not very familiar with Perl.
To use php(as the current mainstream development language) as the Shell script language, you must use php(as the current mainstream development language) as the binary cgi(not many people use it now)compile, instead of apache(the most popular WEB server platform on Unix platform) mode; compile into binary cgi(now Not many people are using it anymore) PHP running in mode (as the current mainstream development language) has some security issues. For solutions, see php (as The current mainstream development language) manual (http://www.php(as the current mainstream development language).net).
You may feel uncomfortable writing Shell scripts at first, but you will get better gradually: use php(as the current mainstream development language) as a general dynamic web page The only difference between the language and the shell script language is that a shell script needs to explain the program path of the script in the first line:
We use php(as the current mainstream development language)The parameter "-1" is added after the execution file, so that php(as the current mainstream development language) will not output HTTPHeader (if you still need to use it as a dynamic web page on the Web, then you need to use it yourself The header function outputs HTTPHeader). Of course, in the Shell script you still need to use the opening and closing tags of php(as the current mainstream development language):
(as the current mainstream development language) Code?>
Now let us look at an example to better understand the use of php (as the current mainstream development language) as a Shell script language Use: (as the current mainstream development language)
print("Hello, world!n");
?>
The above program will be simple Output "Hello, world!" to the display.
1. Pass the Shell script running parameters to php(as the current mainstream development language):
As a Shell script, you will often run the program When adding some parameters, php (as the current mainstream development language) has an embedded array "$argv" when used as a Shell script. Using the "$argv" array can easily read the Shell script Parameters at run time ("$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 current mainstream development language) -q
(as the current mainstream development language )
$first_name = $argv[1];
$last_name = $argv[2];
printf("Hello, %s %s! How are you today?n", $ first_name, $last_name);
?>
When running, the above code requires two parameters, namely last name and first name. For example, run like this:
[dbrogdon@artemis dbrogdon]$ scriptname. ph Darrell Brogdon
The Shell script will output on the monitor:
Hello, Darrell Brogdon! How are you today?
[dbrogdon@artemis dbrogdon]$
In php (as the current When php (as the current mainstream development language) is used as a Shell script, it also contains the "$argv" array. When using the 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 QueryString.
2. Write an interactive Shell script: If a Shell script just runs by itself and loses interactivity, then it is meaningless.When php(as the current mainstream development language) is used to write Shell scripts, how to read the information entered by the user? Unfortunately, php (as the current mainstream development language) itself does not have a function or method for reading user input information, but we can follow other languages and write a function "read" that reads user input information. : (as the current mainstream development language)
function read() {
$fp = fopen(/dev/stdin, r);
$input = fgets($fp, 255);
fclose($fp);
return $input;
}
?>
It should be noted that the above function can only be used on Unix systems (Other systems will require corresponding changes). The above function will open a file pointer, then read a line of no more than 255 bytes (that's what fgets does), then close the file pointer and return the read information.
Now we can use the function "read" to modify the program 1 we wrote earlier to make it more "interactive": (as the current mainstream development language)
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();