Use PHP as the _php base for the Shell scripting language

Source: Internet
Author: User
Tags parse error php code stdin

As we all know, PHP is a very good dynamic web development language (fast, short development cycle ...). )。 But only a few people realize that PHP can also be a good language for writing shell scripts, and when PHP is the language that writes the shell script, he's not as powerful as Perl or bash, but he has a good advantage. Especially for people like me who are familiar with PHP but don't know much about Perl.
To use PHP as the Shell scripting language, you have to compile PHP as a binary CGI, not Apache, and compile into binary CGI mode to run PHP with some security issues, see the PHP Manual for Solutions (http://www.php.net).
At first you might not feel comfortable writing a shell script, but it's getting better: the only difference between writing PHP as a regular dynamic Web page and being a Shell scripting language is that a shell script needs to interpret the script's program path in the first line of life:
We added the parameter "-1" to the PHP execution file so that PHP does not output httpheader (if you still need to be a Dynamic Web page, you need to use the header function to output Httpheader). Of course, in the shell script you still need to use PHP's start and end tags:

<?php Code?>

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

Copy Code code as follows:

<?php
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 a shell script, often add some parameters when running the program, PHP as a shell script with an embedded array "$ARGV", using the "$ARGV" array can easily read the shell script run-time parameters ("$argv [1]" Corresponds to the first argument, "$ARGV [2]" corresponds to the second argument, and so on. For example, the following program:

Copy Code code as follows:

#!/usr/local/bin/php-q
<?php
$first _name = $argv [1];
$last _name = $argv [2];
printf ("Hello,%s%s! How to Are you Today?n ", $first _name, $last _name);
?>

The above code requires two parameters when it is run, which is the last name and first name, such as running:
[Dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon
The shell script will output on the monitor:
Hello, Darrell brogdon! How are are you today?
[Dbrogdon@artemis dbrogdon]$
PHP also contains an array of "$ARGV" when writing language as a Dynamic Web page, however, there are some differences: when PHP as a shell scripting language, "$argv [0]" corresponds to the script file name, and when used for dynamic Web page writing, "$argv [1]" Corresponds to the first parameter of the QueryString.

Second, write an interactive shell script:
If a shell script simply runs on its own and loses interactivity, then it doesn't mean anything. When PHP is used to write shell scripts, how do you read the information entered by the user? Unfortunately, PHP itself does not have a function or method of reading user input information, but we can write a function "read" that reads the user input information in other languages:

Copy Code code as follows:

<?php
function Read () {
$fp = fopen ('/dev/stdin ', ' R ');
$input = fgets ($fp, 255);
Fclose ($FP);
return $input;
}
?>

Note that this function can only be used on UNIX systems (other systems need to be changed accordingly). The above function opens a file pointer and then reads a row that does not exceed 255 bytes (that is, fgets), and then closes the file pointer and returns the read information.
Now we can use the function "read" To modify the program 1 we wrote earlier, making him more "interactive":

Copy Code code as follows:

<?php
function Read () {
$fp = fopen ('/dev/stdin ', ' R ');
$input = fgets ($fp, 255);
Fclose ($FP);
return $input;
}
Print ("What is your 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, and you may see something unexpected: The last line of input turns into three lines! This is because the information returned by the "read" function also includes a newline character "\ n" at the end of each line of the user, retained in the surname and first name, and to remove the ending line break, you need to modify the "read" function:

Copy Code code as follows:

<?php
function Read () {
$fp = fopen ('/dev/stdin ', ' R ');
$input = fgets ($fp, 255);
Fclose ($FP);
$input = Chop ($input); Remove trailing blanks
return $input;
}
?>

Third, the shell script written in other languages contains the shell script written by PHP:
Sometimes we might want to include a shell script written in PHP in a shell script written in another language. Actually very simple, here is a simple example:
The echo is the Bash section of the code.

Copy Code code as follows:

/usr/local/bin/php-q << EOF
<?php
Print ("This is the PHP section of the Coden");
?>
Eof

In fact, it is called PHP to parse the following code and then output; then try the following code:
The echo is the Bash section of the code.

Copy Code code as follows:

/usr/local/bin/php-q << EOF
<?php
$myVar = ' PHP ';
Print ("The $myVar section of the Coden");
?>
Eof

Can be seen two times the only difference is the second time using a variable "$myVar", try to run, PHP unexpectedly give error information: "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, and to solve the problem you need to add the "\" escape character in front of each variable in PHP, and the code is modified as follows:
The echo is the Bash section of the code.

Copy Code code as follows:

/usr/local/bin/php-q << EOF
<?php
\ $myVar = ' PHP ';
Print ("The $myVar section of the Coden");
?>
Eof

OK, now you can write your own shell script in PHP, and hope everything goes well with you. If you have any questions, you can go to the forum for discussion.


English version 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.