PHP can also be used as a shell scriptposted time: 2003-5-9 18:22:46 Author: Flying eagle Click: 4325 Source: www.aspcool.com
PHP can also be used as a shell srcipt
Source: Linuxfab.cx
How is PHP so red?
Recently, PHP (Personal Hypertext Preprocessor) seems to have become the most widely used web page processing language used in Linux / UNIX. It is convenient, powerful function and OpenSource's characteristics make it gradually eroded to traditional CGI or even The market of Microsoft ASP (Active Server Page), almost all major websites recruited talents that do not use PHP as basic conditions.
PHP does have this eligibility to be so red, the reason has the following points:
PHP is OpenSource software, completely free, free to spread, therefore attracting a lot of people to use, because of this, it is attracted to the business company to develop better engine and optimization software (please refer to http: // www .zend.com /).
PHP itself is very easy to understand, shallow instruction syntax, plus some basic object-oriented processing capabilities, so that novices are enough to learn in the shortest time.
PHP provides considerable functions, including mathematical processing, string processing, network-related functions, support, image processing functions, and numerous developers are developing a wide range of new features for PHP development, expand Positive poles.
PHP is very easy to combine with Apache. As the Apache module, the setting is quite simple, because Apache has already occupied 60% of Web Server worldwide, and PHP will naturally become Apache best match.
However, this time, the subject is not the application of PHP on web design, but the application of PHP on Shell Srcipt, the generally known shell srcipt is approximately TCSH, Bash, Perl or Python these languages, I want Talking is to use the PHP as a shell srcipt.
PHP execution file installation
General PHP as a web page handling language is a module to be compiled into apache. Of course, it is nothing, and it is very simple to compile, as long as the root is identified as follows:
Unlock PHP-3.0.xx.tar.gz
CD PHP
CONFIGURE
Make
After compiling, there is an executable file in the PHP directory, which is called PHP, and you can click / usr / local / bin. Note that if the file is too large, you can use the Strip instruction to remove the way the PHP will remove unnecessary information, so that the file will be much smaller.
First program
Start writing our first PHP shell srcipt program, this example prints "Hello World!":
#! / usr / local / bin / php -q
Echo "Hello, World!";
?>
{You can name the file or other names named PHP in this OK, run in Linux ./ file name. If I named shell.php, run ./Shell under Linux. There will be the output, but the Note is to change the property of this file to executable chMOD 755 shell.php} before running
Note that PHP is originally applied in web application, so it will tell HTML's header, but we are going to use PHP as shell srcipt, "- q" means not to send HEADER meaning, you can try it Plus -q's display result. In this example, / usr / local / bin / php is a PHP to perform / usr / local / bin / under, because we just put it in it. The Echo directive prints "Hello, World!", "" "" "characters are wrap characters.
Note that after using this program, it is necessary to convert its CHMOD an executable attribute (CHMOD X file name) before it can be executed.
Advanced use i
Sometimes we need to send some parameters when procedure, such as the instructions of the LS, followed by the -l parameter, PHP shell srcipt also supports such usage, there are two special variables: $ argc record behind The number of parameters, the $ argv [] array parameter is the content of the parameters. For example, I have to design a program for two numbers: #! / Usr / local / bin / php -q
$ SUM = 0;
$ SUM = $ SUM $ Argv [1] $ Argv [2];
Echo $ SUM;
?>
Assume that this program is named sub.php3, then execute Sum.php3 1 2 Press ENTER to print 3.
If you want to calculate the parameters of the unfix number and, you have to use the special variable of $ argc:
#! / usr / local / bin / php -q
$ SUM = 0;
For ($ T = 1; $ t <= $ argc; $ t )
$ SUM = $ SUM $ Argv [$ T];
Echo $ SUM;
?>
Assume that this program is named BigSum.php3, then execute BigSum.php3 1 2 3 4 5 Press ENTER to print 15, execute BigSum.php3 1 2 3 4 5 6 Press ENTER to print 21.
Sometimes we need to enter information in the program execution, but PHP is originally used for web design, and the information input on the web is naturally entered in the way, so this will come as shell srciPt, good In the PHP, there is an open file function, and under Linux / UINX, the input (INPUT) can be done in the way, we want to open / dev / stdin device file (stdin is Representing Standard Input means), the program is as follows:
#! / usr / local / bin / php -q
$ fp = fopen ("/ dev / stdin", "r");
$ INPUTSTR = FGETS ($ FP, 100);
Fclose ($ fp);
Echo "/ N -------------------- / N";
ECHO $ INPUTSTR;
?>
The FGETS ($ fp, 100) refers to the file from $ fp (that is, "/ dev / stdin") to read 100 Byte information, the program will stop waiting for our input, When we enter the Enter, the program will print the information we have entered.
Advanced use II
Although the input is already handled, this function is obviously too simple, and it is impossible to cope with a larger application. For example, I need a function to remove HTML in a string stream (Data Stream) to remove, then need completely Processing the output of output input steering, 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 execute this program directly, it will wait for you to enter until you press Ctrl D, you will print your input information, we can do it like this:
More filt.php3 | filt.php3
This way is to show the file with the file with more to Filt.php3. Filt.php3 will constantly accept the information (in fact, Filt.php3 program itself), and finally
We can add the function of filtering HTML:
#! / 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;
?>
Suppose this program is named filt2.php3, so that the filter is completed, do not believe, take an HTML file to try:
More xxx.html | filt2.php3
You will see the file that deletes the HTML Tag.
in conclusion
PHP is used to use the shell srcipt. The reason is that the PHP itself is very good, and it supports various databases. When you have often take PHP to design your website, it will absolutely don't like it. The shell srcipt language handles other parts that must be non-pages. At this time, take PHP to do shell srcipt. It will appear. You can develop the entire system in a consistent way, and don't have to use PHP and use Perl. / Python or C.
Domestic PHP wind is quite prosperous, this site Linuxfab is completely developed with MySQL with PHP, in fact, there is still a lot of PHP, there is a chance to introduce, if readers need PHP related information, welcome to this site The PHP Forum participated in more discussion.