Use PHP as shell scripting language

zhaozj2021-02-16  47

Use PHP as shell scripting language

- English original: Darrell Brogdon, Posted on http://www.phpbuilder.com/columns/darrell20000319.php3) Many people have thought about writing some timed-time letter from PHP, but there is no way to perform PHP When I went to PHPBUILDER, I found this article, so I would like to translate it (I have made some modifications), I hope to be useful to everyone. For the first translation article, please don't forgive me. -------------------------------------------------- -------------------------------- We all know that PHP is a very good dynamic web development language (speed speed The development cycle is short ...). But only a small number of people realized that PHP can also be very good as a language written shell script. When PHP is written as a language written shell script, he doesn't have Perl or Bash so powerful, but he has a good advantage. Especially those who are familiar with PHP but not very familiar with Perl. To use PHP as a shell scripting language, you must compile PHP as binary CGI, not Apache mode; compiled PHPs with binary CGI mode have some security issues, and see the PHP manual for solving (http: / / www.php.net). At the beginning, you may feel uncomfortable to write shell scripts, but will slowly: Writing PHP as a general dynamic web page and as a shell scripting language is that a shell script needs to explain in the first line of life The program path of this script: #! / Usr / local / bin / php -q We added parameters "-1" after the PHP executive file, so that PHP will not output httpheader (if still need to be a web page, Then 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: Now let's take an example so that you can better understand the use of PHP as a shell scripting language: #! / Usr / local / bin / php -q The above program will simply output "Hello, World!" to the display. First, pass the shell script running parameter to PHP: as a shell script, often add some parameters when running the program, PHP has an embedded array "$ argv" when the PHP is used as a shell script, which can be very convenient to use the "$ argv" array. Read the parameter of the shell script time ("$ argv [1]" corresponds to the first parameter, "$ argv [2]" corresponds to the second parameter, and so on.

For example, the program below: #! / Usr / local / bin / php -q The above code requires two parameters when running, respectively, named, such as this run: [dbrogdon @ artemis dbrogdon] $ Scriptname.ph Darrell The brogdon shell script will output on the display: Hello, Darrell Brogdon! How are you today? [Dbrogdon @ artemis dbrogdon] $ In PHP as a dynamic webpage writing language, it also contains "$ argv" arrays, but there are some differences here. : When PHP is called as a shell scripting language, "$ argv [0]" corresponds to the file name of the script, and when the dynamic web page is written, "$ argv [1]" corresponds to the first parameter of querystring. . Second, write a interactive shell script: If a shell script is just running itself, there is nothing to do so much. How to read the information input when the PHP is written for the shell script? Unfortunately, PHP does not read the function or method of the user input information, but we can follow the other language to write a function "read" "read" that read the user input information: Need to note that the above function can only be used for UNIX systems (other The system needs to make a corresponding change). The above function opens a file pointer and then reads a row that does not exceed 255 bytes (that is, the role of FGETS), then turn off the file pointer, returns the read information.

Now we can use the function "read" to modify the programs you wrote before, making him more "interactive": #! / Usr / local / bin / php -q The program is saved, run, you may see an expected thing: the last line of input becomes three lines! This is because the information returned by the "read" function also includes the end of the user's end of the user, retains the last name and name, to remove the end of the end, you need to modify the "read" function: 3, the shell script containing PHP in other languages: Sometimes we need to include PHL scripts written in other languages.

转载请注明原文地址:https://www.9cbs.com/read-26639.html

New Post(0)