Use PHP as shell scripting language

xiaoxiao2021-03-06  65

Use PHP as shell scripting language

Many people have thought about using PHP to write some scheduled programs, but there is no way to execute PHP; when I go to PHPBUILDER, I discovered this article, so I want to translate it (I have made some modifications at the same time. ),hope its good for U.S. For the first translation article, please don't forgive me.

-------------------------------------------------- --------------------------------

We all know that PHP is a very good dynamic web development language (fast speed, short development cycle ...). 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 joined the parameters "-1" after the PHP executive file, so that PHP will not output httpheader (if you still need a dynamic web page of the web, then you need to use the Header function to output httpheader). Of course, you still need to use PHP's start and end tags inside the shell script:

Let us now look at an example so that you can 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 display.

First, pass the shell script to run parameters to PHP:

As a shell script, there is often an embedded array "$ argv" when the PHP is used as the shell script, and the "$ argv" array can be easily read the parameters of the shell script time ( "$ Argv [1]" corresponds to the first parameter, "$ argv [2]" corresponds to the second parameter, and so on. For example, this program:

#! / usr / local / bin / php -q

$ first_name = $ argv [1];

$ last_name = $ argv [2];

Printf ("Hello,% S% S! How are you today? / n", $ first_name, $ last_name);

?>

The above code requires two parameters when running, respectively, the name and name, such as this run:

[DBROGDON @ artemis dbrogdon] $ ScriptName.ph Darrell Brogdon

The shell script will output on the display:

Hello, Darrell Brogdon! How Are you today?

[DBROGDON @ artemis dbrogdon] $ ARGV "array when PHP is written as a dynamic web page, but there are some differences here:" $ argv [0] "corresponds to" when PHP is the shell scripting language " The file name of the script, while when the dynamic web page is written, "$ argv [1]" corresponds to the first parameter of querystring.

Second, write an 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 itself does not read functions or methods of user input information, but we can follow other languages ​​to write a function "Read" that reads user input information:

Function read () {

$ fp = fopen ('/ dev / stdin', 'r');

$ INPUT = FGETS ($ FP, 255);

Fclose ($ fp);

Return $ INPUT;

}

?>

It should be noted that the above function can only be used for UNIX systems (other systems need to be changed). 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 we wrote before, making him more "interactivity":

#! / 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 is your last name?");

$ last_name = read ();

Print ("/ NHELLO, $ FIRST_NAME $ Last_name! Nice to meet you! / n");

?>

Save the above program, 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 ends of the online line, retains to the last name and name, to remove the end of the end, you need to modify the "Read" function:

Function read () {

$ fp = fopen ('/ dev / stdin', 'r');

$ INPUT = FGETS ($ FP, 255);

Fclose ($ fp);

$ INPUT = Chop ($ INPUT); // Remove the tail blank

Return $ INPUT;

}

?>

Third, the shell script containing PHP in other languages:

Sometimes we may need to include the shell script written in the shell script in other languages. In fact, it is very simple. Here is a simple example:

#! / bin / bash

Echo this is the bash section of the code.

/ usr / local / bin / php -q << EOF

Print ("this is the pHP section of the code / n");?>

EOF

In fact, call PHP to parse the following code, then output; then, try the following code:

#! / bin / bash

Echo this is the bash section of the code.

/ usr / local / bin / php -q << EOF

$ myvar = 'php';

Print ("this is the $ myvar section of the code / n");

?>

EOF

It can be seen that the only difference between the code is the second time I use a variable "$ myvar", try running, PHP actually gives an error message: "PARSE Error: Parse Error in - on line 2"! This is because the variables in Bash are also "$ myvar", and the Bash parser will replace the variables first, to solve this problem, you need to add "/" escape in front of each PHP variable, then The code just revised as follows:

#! / bin / bash

Echo this is the bash section of the code.

/ usr / local / bin / php -q << EOF

/ $ myvar = 'php';

Print ("this is the / $ myvar section of the code / n");

?>

EOF

Ok, now you can write your own shell script with PHP, I hope you all go well. If you have any questions, you can go to http://www.phpbuilder.com or http://www.zphp.com.

English address: http://www.phpbuilder.com/columns/darrell20000319.php3

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

New Post(0)