SoFunction
Updated on 2025-04-08

Use PHP as Shell scripting language

We all know that PHP is a very good dynamic web development language (fast speed, short development cycle...). But only a few people realize that PHP can also be a good language for writing shell scripts. When PHP is a language for writing shell scripts, it is not as powerful as Perl or Bash, but it has good advantages, especially for people like me who are familiar with PHP but not very familiar with Perl.
To use PHP as the Shell scripting language, you must compile PHP as binary CGI, not Apache mode; there are some security issues in compiling PHP running in binary CGI mode, and you can refer to the PHP manual ().
At first you may feel uncomfortable writing shell scripts, but it will get better: the only difference between using PHP as a general dynamic webpage writing language and as a shell scripting language is that a shell script needs to interpret the program path of the script in the first line:
We added the parameter "-1" after the PHP execution file, so that PHP will not output HTTPHeader (if you still need to use the header function to output HTTPHeader). Of course, you still need to use the PHP start and end tags in the shell script:

<?php code ?>

Now let's look at an example to better understand the use of PHP as the Shell scripting language:

Copy the codeThe code is as follows:

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

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

1. Pass the shell script running parameters to PHP:
As a shell script, some parameters are often added when running the program. When PHP is a shell script, it has an embedded array "$argv". Using the "$argv" array can easily read the parameters of the shell script runtime ("$argv[1]" corresponds to the first parameter, "$argv[2]" corresponds to the second parameter, and so on). For example, the following program:

Copy the codeThe code is as follows:

#!/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);
?>

When running the above code, two parameters are required, namely the last name and the first name, for example, run like this:
[dbrogdon@artemis dbrogdon]$ Darrell Brogdon
The shell script will output on the monitor:
Hello, Darrell Brogdon! How are you today?
[dbrogdon@artemis dbrogdon]$
When PHP is a dynamic web page writing language, it also contains the array "$argv", but it is a little different from here: when PHP is a shell scripting language, "$argv[0]" corresponds to the file name of the script, and when used for dynamic web page writing, "$argv[1]" corresponds to the first parameter of QueryString.

2. Write an interactive shell script:
If a shell script is simply run on its own and loses its interactivity, then there is no meaning. 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 to read user input information, but we can follow other languages ​​to write a function "read" that reads user input information:

Copy the codeThe code is as follows:

<?php
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 in Unix systems (other systems need to make corresponding changes). The above function will open a file pointer, then read a line that does not exceed 255 bytes (that is, what fgets does), then close the file pointer, and return the read information.
Now we can use the function "read" to modify the program 1 we wrote earlier to make it more "interactive":

Copy the codeThe code is as follows:

<?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 input to the last line becomes three lines! This is because the information returned by the "read" function also includes the ending line break "\n" of each line of the user, which is retained in the last name and name. To remove the ending line break, the "read" function needs to be modified:

Copy the codeThe code is as follows:

<?php
function read() {
$fp = fopen('/dev/stdin', 'r');
$input = fgets($fp, 255);
fclose($fp);
$input = chop($input); // Remove the tail blank
return $input;
}
?>

3. Shell scripts written in other languages ​​include shell scripts written in PHP:
Sometimes we may need to include shell scripts written by PHP in shell scripts written in other languages. It's actually very simple. Here is a simple example:
echo This is the Bash section of the code.

Copy the codeThe code is as follows:

/usr/local/bin/php -q << EOF
<?php
print("This is the PHP section of the coden");
?>
EOF

In fact, it is to call PHP to parse the following code and then output it; then try the following code:
echo This is the Bash section of the code.

Copy the codeThe code is as follows:

/usr/local/bin/php -q << EOF
<?php
$myVar = 'PHP';
print("This is the $myVar section of the coden");
?>
EOF

It can be seen that the only difference between the codes two times is that the second time I used a variable "$myVar". Try running it, and PHP actually gave the error message: "Parse error: parse error in - on line 2"! This is because the variables in Bash are also "$myVar", and the Bash parser replaces the variable first. To solve this problem, you need to add the "\" escape character before each PHP variable. Then the code just now is modified as follows:
echo This is the Bash section of the code.

Copy the codeThe code is as follows:

/usr/local/bin/php -q << EOF
<?php
\$myVar = 'PHP';
print("This is the \$myVar section of the coden");
?>
EOF

OK, now you can write your own shell scripts in PHP, and hope you'll be well. If you have any questions, you can discuss it in this forum.


English version address: /columns/darrell20000319.php3