Use PHP as a shell scripting language

Source: Internet
Author: User
Tags parse error

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.
To use PHP as the shell scripting language, you must compile php as a binary CGI instead of the Apache mode. 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 page writing language and using shell scripting language is that a shell script needs to be interpreted in the first line of life.ProgramPath:
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:

<? PHPCode?>

Now let's look at an example to better understand the use of PHP as a shell scripting language:

Copy code The Code is as follows: <? PHP
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:

Copy code The Code is as follows :#! /Usr/local/bin/PHP-Q
<? PHP
$ First_name = $ argv [1];
$ Last_name = $ argv [2];
Printf ("Hello, % S % 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 ":

Copy code The Code is as follows: <? PHP
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:

Copy code The Code is as follows: <? PHP
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 ();
Print ("nhello, $ first_name $ last_name! Nice to meet you! N ");
?>

Save the above program and run it. You may see an unexpected thing: the input in the last line is changed to three lines! This is because the information returned by the "read" function also includes the line break "\ n" at the end of each row of the user, which is retained to the surname and name, and the line break at the end must be removed, modify the "read" function:

Copy code The Code is as follows: <? PHP
Function read (){
$ Fp = fopen ('/dev/stdin', 'R ');
$ Input = fgets ($ FP, 255 );
Fclose ($ FP );
$ Input = chop ($ input); // Remove trailing space
Return $ input;
}
?>

3. shell scripts written in other languages include shell scripts written in PHP:
Sometimes we may need to include shell scripts written in PHP in shell scripts written in other languages. It is actually very simple. Here is a simple example:
Echo this is the bash section of the Code.

Copy code The Code is as follows:/usr/local/bin/PHP-q <EOF
<? PHP
Print ("this is the PHP section of the coden ");
?>
EOF

In fact, it is to call PHP to parse the following code and then output it; then, try the following code:
Echo this is the bash section of the Code.

Copy code The Code is as follows:/usr/local/bin/PHP-q <EOF
<? PHP
$ Myvar = 'php ';
Print ("this is the $ myvar section of the coden ");
?>
EOF

We can see that the only difference between the two codes is that a variable "$ myvar" is used for the second time. If you try to run the code, PHP returns the error message: "Parse error: parse error in-on line 2 "! This is because the variable in Bash is also "$ myvar", and the bash parser replaces the variable first. To solve this problem, you need to add the "\" Escape Character before each PHP variable. The Code just modified is as follows:
Echo this is the bash section of the Code.

Copy code The Code is as follows:/usr/local/bin/PHP-q <EOF
<? PHP
\ $ Myvar = 'php ';
Print ("this is the \ $ myvar section of the coden ");
?>
EOF

Now, you can use PHP to write your own shell script. I hope everything goes well. If you have any questions, go to the Forum.

English address: http://www.phpbuilder.com/columns/darrell20000319.php3

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.