Shell Script Way php php how so Red recently PHP (Personal hypertext preprocessor) seems to have been the most widely used in the last two years Linux/unix the Web processing language, its convenient, powerful features and O Pensource's features make it increasingly eroding into traditional CGI or even the MicroSoft ASP (Active Server Page) market, with almost every major website recruiting people to use PHP as a basic condition. PHP does have this qualification can be so red, for the following points: PHP is opensource software, completely free and free to spread, so it attracts a lot of people to use, and because of this, to attract commercial companies to develop better engine and optimization software (please refer to http:// www.zend.com/). PHP itself is very easy to understand, simple instruction syntax, plus some basic object-oriented processing ability, so that novice enough to learn in the shortest time. PHP provides a considerable amount of functionality, including math processing, string processing, network-related functions, various database support, image processing capabilities, a large number of the development of PHP is developing a variety of new features, extensibility is excellent. PHP is very easy to combine with Apache, as the Apache module to use, set up a very simple installation, also because Apache has now occupied the Web Server Global 60% Market,php naturally become the best combination of Apache. However, this time to talk about the topic is not PHP in the application of Web design, but PHP in Shell script application, generally known shell script is about TCSH, bash, perl or Python these languages, all I want to talk about is the PHP Used as Shell Script. PHP execution file installation generally PHP as a web processing language is to be compiled into Apache module, here of course not to do, it is easy to compile, as long as root as the following actions: Unlock php-3.0.xx.tar.gz CD PHP Configure After make compiles, there is an executable file in the PHP directory with the file name php, copy it to/usr/local/bin. Note that if the file is too large, you can use the strip instruction to remove unnecessary information in PHP's way, so that the file is much smaller. The first program starts writing our first PHP Shell Script Program, this example prints "Hello world!": #!/usr/local/bin/php-q Notice that PHP was originally applied to the Web application, so it will send out the HTML header, but here we are to use PHP as Shell script, "-Q" means not to send the HEADER meaning, you can try not to add-Q display results. In this example,/usr/local/bin/php means to execute the php, under/usr/local/bin/because we just loaded it there. The echo command prints "Hello, world!", where the "" character is a newline character. Note that after you save this program as a file, you must chmod it as an executable property (chmod +x filename) before you can execute it. Advanced use I Sometimes we need to send some parameters when the program executes, such as LS, which can be appended with the-l parameter,php Shell Script, there are two special variables: $ARGC records the number of arguments that are fed back, $ARGV [] Array parameter is the contents of the parameter. For example, I'm going to design a program that calculates the sum of two numbers: #!/usr/local/bin/php-q Assuming that the program is named sum.php3, then execute SUM.PHP3 1 2 press ENTER to print 3. If you want to figure out a specific number of parameters and, then you need to use the $ARGC this special variable: #!/usr/local/bin/php-q Assuming this program is named bigsum.php3, then execute BIGSUM.PHP3 1 2 3 4 5 Press ENTER to print out 15, execution bigsum.php3 1 2 3 4 5 6 Press ENTER to print 21. Sometimes we need to enter data in the program execution, but PHP is originally used for web design, and the data input on the Web page is naturally used as a form to input, so this will php as a Shell Script when the problem comes, fortunately PHP has provided open file function, and in linux/u INX, input this thing can be done in the way of the open file, we want to open the/dev/stdin this device file (stdin is the meaning of standard input), the program is as follows: #!/usr/local/bin/php-q One of the fgets ($FP, 100) refers to the $fp this file (that is, "/dev/stdin") read out 100 byte of the data, the program execution to this line will stop to wait for our input, when we enter the end of the press ENTER, the program will be the data we have just entered Printed out. Advanced use II Although I can already handle the input, but such a function is obviously too simple to cope with larger applications, for example, I need a function is to remove a string of data stream in the HTML, then need to fully handle the output input steering ability, We can first design the program as follows: #!/usr/local/bin/php-q Suppose this program is named filt.php3, if you execute the program directly, it will wait for you to enter it until you press Ctrl+d to print out your input data, we can do it: more Filt.php3 | Filt.php3 This practice is to filt.php3 this program with more to show and turn to filt.php3 this program,FILT.PHP3 will continue to receive information (in fact, FILT.PHP3 program code itself), and finally printed it out. We can add the ability to filter HTML: #!/usr/local/bin/php-q
http://www.bkjia.com/PHPjc/531741.html www.bkjia.com true http://www.bkjia.com/PHPjc/531741.html techarticle Shell Script Way php php how so Red recently PHP (Personal hypertext preprocessor) seems to have been the most widely used in the last two years Linux/unix the Web Processing language ...