How to use PHP as the Shell scripting language

Source: Internet
Author: User
Tags parse error
As we all know, PHP is a very good dynamic web development language (fast, short development cycle ...). )。 But only a very small number of people realize that PHP can also be very good as the language of writing shell scripts, when PHP as the language of writing shell scripts, he is not as powerful as Perl or bash, but he has a good advantage, Especially for those of me who are familiar with PHP but are not familiar with Perl.
to use PHP as the Shell scripting language, you have to compile PHP as a binary CGI instead of Apache, and there are some security issues with PHP compiled into binary CGI mode, which can be found in the PHP Manual (http://www.php.net).
you might not get used to writing shell scripts at first, but it will get better: the only difference between PHP as a generic Dynamic Web page and as a Shell scripting language is that a shell script needs to explain the script's program path in the first line of life:
#!/usr/local/bin/php-q
we add the parameter "1" After the PHP execution file, so that the child PHP will not output httpheader (if you still need to be a Dynamic Web page, then you need to use the header function output Httpheader). Of course, in the shell script you still need to use PHP's start and end tags:
Now let's look at an example to better understand the use of PHP as a shell scripting language:
#!/usr/local/bin/php-q
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 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-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]$
PHP also contains the "$ARGV" array when it is written as a dynamic Web page, but there are some differences: when PHP is the Shell scripting 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 the 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 is used for shell script writing, how to read the user input information? Unfortunately, PHP itself does not have a function or method to read user input information, but we can follow other languages to write a function that reads user input information "read":
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":
#!/usr/local/bin/php-q
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 ();
print ("\nhello, $first _name $last _name! Nice to meet you!\n ");
?>
Save the above program and run it, you may see something unexpected: The last line of input becomes three lines! This is because the information returned by the "read" function also includes the end-of-line newline character "\ n" for each row of the user, which is retained in the first and last names, and the "read" function needs to be modified by removing the end of the line break:
function Read () {
$fp = fopen ('/dev/stdin ', ' R ');
$input = fgets ($fp, 255);
fclose ($fp);
$input = Chop ($input);//Remove trailing blanks
return $input;
}
?>
C. Shell scripts written in Other languages include shell scripts written by PHP:
Sometimes we may need to include shell scripts written by PHP in shell scripts written in other languages. Actually very simple, here is a simple example:
#!/bin/bash
Echo this was the Bash section of the code.
/usr/local/bin/php-q << EOF
print ("This was the PHP section of the code\n");
?>
EOF
In fact, it is called PHP to parse the following code, and then output; then try the following code:
#!/bin/bash
Echo this was the Bash section of the code.
/usr/local/bin/php-q << EOF
$myVar = ' PHP ';
print ("This was the $myVar section of the code\n");
?>
EOF
you can see that the only difference in code two times is the second time using a variable "$myVar", try running, PHP actually gave the error message: "Parse error:parse error in-on line 2"! This is because the variables in bash are also "$myVar", and the bash parser replaces the variables first, and to solve this problem, you need to precede each PHP variable with a "\" escape character, then the code is modified as follows:
#!/bin/bash
Echo this was the Bash section of the code.
/usr/local/bin/php-q << EOF
\ $myVar = ' PHP ';
print ("This was the \ $myVar section of the code\n");
?>
EOF
Well, now you can write your own shell script in PHP, and I hope everything goes well with you.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes how to use PHP as a shell scripting language, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.