New method: Use PHP as the Shell scripting language _php Tutorial

Source: Internet
Author: User
As we all know, PHP (as the current mainstream development language) is a very good dynamic web development language (fast, short development cycle ...). )。 But only a very small number of people realize that PHP (as the current mainstream development language) can also be very good as the language of writing shell scripts, when PHP (as the current mainstream development language) as the language of writing shell scripts, he is not as powerful as Perl or bash, But he has a very good advantage, especially for me, who is familiar with PHP (which is now the mainstream development language) but not very familiar with Perl.
  
To use PHP (as the current mainstream development language) as the Shell scripting language, you have to compile PHP (which is now the mainstream development language) as a binary CGI (now not many people are using), rather than Apache (the most popular Web server platform for UNIX platforms) mode Compiled into binary CGI (now not many people in use) mode to run PHP (as the current mainstream development language) there are some security issues, about the solution can be found in PHP (as the current mainstream development language) manual (http://www.php (as the mainstream development language now) ). net).
  
At first you might not get used to writing shell scripts, but it will get better: PHP (as the current mainstream development language) The only difference between a typical Dynamic Web page authoring language and a 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 PHP (as the current mainstream development language), so that PHP (as the current mainstream development language) does not output httpheader (if it still needs to be a Dynamic Web page, Then you need to use the header function output Httpheader yourself). Of course, in the shell script you still need to use PHP (as the current mainstream development language) start and end tags:
  
  
Now let's take a look at an example to get a better understanding of the use of PHP as a Shell scripting language for today's mainstream development language: 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 the current mainstream development language):
As a shell script, often add some parameters when running the program, PHP (as the current mainstream development language) 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: #!/usr/local/bin/php (as the current mainstream development language)-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]$
The "$ARGV" array is also included in PHP (the current mainstream development language) as a dynamic Web language, but it differs from this: when PHP (as the mainstream development language of today) is the Shell scripting language, "$argv [0]" corresponds to the file name of the script, When used for dynamic Web page writing, "$argv [1]" corresponds to the first parameter of 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 (as the current mainstream development language) is used for the writing of shell scripts, how to read the user input information? Unfortunately, PHP, which is now the mainstream development language, does not have a function or method of reading user input information, but we can write a function "read" that reads user input information in other languages: 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": 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 ();

http://www.bkjia.com/PHPjc/509136.html www.bkjia.com true http://www.bkjia.com/PHPjc/509136.html techarticle As we all know, PHP (as the current mainstream development language) is a very good dynamic web development language (fast, short development cycle ...). )。 But only a very small number of people realize ...

  • Related Article

    Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.