Linux programming uses PHP as a shell script

Source: Internet
Author: User
Tags stdin

As we all know, PHP is a very good dynamic web development language (fast, short development cycle ...). )。 But only a few people realize that PHP can also be a good language for writing shell scripts, and when PHP is the language that writes the shell script, he's not as powerful as Perl or bash, but he has a good advantage. Especially for people like me who are familiar with PHP but don't know much about Perl.

To use PHP as the Shell scripting language, you have to compile PHP as a binary CGI instead of Apache; there are some security issues with compiling PHP into binary CGI mode. At first you might not feel comfortable writing a shell script, but it's getting better: the only difference between writing PHP as a regular dynamic Web page and being a Shell scripting language is that a shell script needs to interpret the script's program path in the first line of life.

We added the parameter "-1" to the PHP execution file so that PHP does not output httpheader (if you still need to be a Dynamic Web page, 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 look at an example to better understand the use of PHP as a shell scripting language:

<?php
print("Hello, world!n");
?>

The above program will simply output "Hello, world!" to the monitor.

First, pass the shell script run parameters to PHP:

As a shell script, often add some parameters when running the program, PHP as a shell script with an embedded array "$ARGV", using the "$ARGV" array can easily read the shell script run-time parameters ("$argv [1]" Corresponds to the first argument, "$ARGV [2]" corresponds to the second argument, and so on. For example, the following program:

#!/usr/local/bin/php -q
<?php
$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 it is run, which is the last name and first name, such as running:

[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon

The shell script will output on the monitor:

Hello, Darrell Brogdon! How are you today?
[dbrogdon@artemis dbrogdon]$

PHP also contains an array of "$ARGV" when writing language as a Dynamic Web page, however, there are some differences: when PHP as a shell scripting language, "$argv [0]" corresponds to the script file name, and when used for dynamic Web page writing, "$argv [1]" Corresponds to the first parameter of the QueryString.

Second, write an interactive shell script:

If a shell script simply runs on its own and loses interactivity, then it doesn't mean anything. When PHP is used to write shell scripts, how do you read the information entered by the user? Unfortunately, PHP itself does not have a function or method of reading user input information, but we can write a function that reads user input information in other languages:

“read”: <?php
function read() {
$fp = fopen('/dev/stdin', 'r');
$input = fgets($fp, 255);
fclose($fp);
return $input;
}
?>

Note that this function can only be used on UNIX systems (other systems need to be changed accordingly). The above function opens a file pointer and then reads a row that does not exceed 255 bytes (that is, fgets), and then closes the file pointer and returns the read information.

Now we can use the function "read" To modify the program 1 we wrote earlier, making him more "interactive":

<?php
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 and run it, and you may see something unexpected: The last line of input turns into three lines! This is because the information returned by the "read" function also includes a newline character "\ n" at the end of each line of the user, retained in the surname and first name, and to remove the ending line break, you need to modify the "read" function:

<?php
function read() {
$fp = fopen('/dev/stdin', 'r');
$input = fgets($fp, 255);
fclose($fp);
$input = chop($input); // 去除尾部空白
return $input;
}
?>

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.