PHP can also be used as a shell script.

Source: Internet
Author: User
Tags echo command html header

Install PHP Execution files

Generally, PHP, as a web processing language, must be compiled into an Apache module. Of course, this is not the case here. It is also very easy to compile, as long as the following actions are performed as root:
Unlock php-3.0.xx.tar.gz
CD PHP
Configure
Make
After compilation, there is an executable file under the PHP Directory named PHP, copy it to/usr/local/bin. Note: If the file size is too large, you can use the Strip command to remove unnecessary information from PHP, so that the file size will be much smaller.
First Program
Start to write our first PHP shell script program. This example shows "Hello world! ":
#! /Usr/local/bin/PHP-Q
<?
Echo "Hello, world! ";
?>
Note that PHP is originally applied on a webpage, so it will certainly send the HTML header, but here we want to use PHP as a shell script, "-Q" means not to send the header if-Q is not added, try again.
In this example,/usr/local/bin/PHP indicates to execute PHP under/usr/local/bin/, because we just installed it here. The echo command will be "Hello, world! "Printed, where" "is a line break character.
Note that after saving the program as a file, you must convert its chmod into an executable attribute (chmod + X file name) before executing it.

Advanced use I
Sometimes we need to input some parameters during program execution, such as the LS command. The-l parameter can be added later. php shell script also supports this usage and has two special variables.: $ argc records the number of input parameters. $ argv [] array parameters are stored in the parameter content. For example, I want to design a program to calculate the sum of two numbers:
#! /Usr/local/bin/PHP-Q
<?
$ Sum = 0;
$ Sum = $ sum + $ argv [1] + $ argv [2];
Echo $ sum;
?>
Assume that the program is named sum. php3. Then, execute sum. php3 1 2 and press enter to print 3.
If you want to calculate the Parameter Sum of an unspecified number, you need to use the special variable $ argc:
#! /Usr/local/bin/PHP-Q
<?
$ Sum = 0;
For ($ T = 1; $ T <= $ argc; $ t ++)
$ Sum = $ sum + $ argv [$ T];
Echo $ sum;
?>
Assume that the program is named bigsum. php3, bigsum is executed. php3 1 2 3 4 5 Press enter to print 15 and execute bigsum. php3 1 2 3 4 5 6 press enter to print 21.
Sometimes we need to input data in the program execution, but PHP was originally used for webpage design, and the data input on the webpage is naturally input using form, so PHP is used as shell script the problem arises. Fortunately, PHP provides the file opening function, while under Linux/Uinx, input (input) you can use the archive method to enable/dev/stdin (stdin indicates standard input). The procedure is as follows:
#! /Usr/local/bin/PHP-Q
<?
$ Fp = fopen ("/dev/stdin", "R ");
$ Inputstr = fgets ($ FP, 100 );
Fclose ($ FP );
Echo "\ n ---------------------- \ n ";
Echo $ inputstr;
?>
Fgets ($ FP, 100) indicates the file from $ FP (that is, "/dev/stdin ") read 100 bytes of data, and the program will stop waiting for our input. After we press enter, the program will print the information we just entered yes.
Advanced Application II
Although the input can be processed, such a function is obviously too simple to cope with larger applications. For example, I need a function to process a series of data streams) in this case, we need to completely process the output input and output redirection capabilities. We can first design the program as follows:
#! /Usr/local/bin/PHP-Q
<?
$ Fp = fopen ("/dev/stdin", "R ");
While (! Feof ($ FP )){
$ C = fgetc ($ FP );
$ Inputstr = $ inputstr. $ C;
};
Fclose ($ FP );
Echo $ inputstr;
?>
Assume that this program is named filt. php3: If you directly execute this program, it will wait for you to enter it until you press Ctrl + D to print your input data. We can execute it like this:
More filt. php3 | filt. php3
In this way, the Filt. the php3 program uses more to show and turn to filt. php3, Filt. php3 will continue to accept materials (in fact, it is filt. php3 program code itself), and finally print it out.
We can add the HTML filter function in it:
#! /Usr/local/bin/PHP-Q
<?
$ Fp = fopen ("/dev/stdin", "R ");
While (! Feof ($ FP )){
$ C = fgetc ($ FP );
$ Inputstr = $ inputstr. $ C;
};
Fclose ($ FP );
$ Inputstr = ereg_replace ("<([^ <>] *)>", "", $ inputstr );
Echo $ inputstr;
?>
Assume that the program is named filt2.php3, so the filtering function is completed. If you do not believe this, please try it with an HTML file:
More xxx.html | filt2.php3
You will see the file that deleted the HTML Tag.

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.