Using PHP as a Shell scripting language _ PHP Tutorial

Source: Internet
Author: User
Tags parse error
Use PHP as the Shell scripting language. 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 well written. 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 writing language and using Shell scripting language is that a Shell script needs to explain the program path of this script in the first line of life:
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:

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

The code is as follows:


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:

The code is as follows:


#! /Usr/local/bin/php-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 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 ":

The code is as follows:


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:

The code is 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 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:

The code is as follows:


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.

The code is as follows:


/Usr/local/bin/php-q <EOF
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.

The code is as follows:


/Usr/local/bin/php-q <EOF
$ 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.

The code is as follows:


/Usr/local/bin/php-q <EOF
\ $ 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

Middleware (fast speed, short development cycle ......). However, only a few people realize that PHP can also be well written...

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.