Tips and Tricks: PHP Shell Scripts

发现在php Interactive shell下,无法使用标准输入和标准输出,只能放在php文件中通过cli模式执行,查了一下资料。

Overview
One of the many features of PHP that is over looked is utilizing PHP as a shell language (just like you can use perl). Many times I find that developers will create cron jobs that utilize wget or lynx to fetch the script and process it. This is not only a bad idea as the script resides in a publicly accessible location but it is more so a bad idea as it doesn’t utilize some of the great features things that PHP will automatically do for you in your configuration files. These things are automatically done by the PHP CLI, if not enabled you can configure PHP with the –enable-cli flag.

Features
There are many features that you can utilize by using this as a shell script as I am outlining below. Some of these are simply php.ini changes that will happen by default and others are simply ways to retrieve information. Essentially what you can do using this tool is unlimited. There is even an Ncurses extension with documentation.

Default Configuration Changes
html_errors is changed to false. So you do not see html error messages on the command line and instead get nicely printed regular error messages (this does not mean if you put in html for error messages that it will not show up)!

implicit_flush is changed to true. Since we are on the command line, chances are that you do not want to buffer the content. If you have a need to buffer the content, this is still possible utilizing output buffering.

max_execution_time is changed to an unlimited time frame. Since you are likely not using this for a web site the data doesn’t need to time out for quick serving as it is most likely some form of utility script that was written.

register_argc_argv is changed to true. Typically one of the frequently asked questions is how to get input when calling a script from the command line. Well argc and argv will give you the solution you need. $argc (integer) will hold the number of arguments that were passed in and $argv (array) will hold the values. The keys of $argv are numeric.

Creating an Executable Script
To create an executable script the permissions on the script must have +x for executable and also have the path of php in the interpreter line (1st line of the file). For example:
Permissions: chmod +x script_name.php.
Interpreter Line: #!/path/to/php.
Executing: ./script_name in the current directory.

STDIN/STDOUT/STDERR
In the PHP CLI, STDIN, STDOUT and STDERR are already initialized, therefore it is rather simple to fetch and write data mindlessly. Each of these are defined by constants that can be utilized with the stream functions (fwrite, fgets, fseek, fscanf, etc). Here are a few examples:

Retrieve and Output Information:

[php]
#!/path/to/php
< ?php
fwrite(STDOUT, "Please enter in some input: ");
$line = trim(fgets(STDIN));
fwrite(STDOUT, $line . "\r\n");
?>
[/php]
Retrieve Information, Check for Errors and Output:

[php]
#!/path/to/php
< ?php
fwrite(STDOUT, "Please enter in an alphanumeric string: ");
$line = trim(fgets(STDIN));
if (!ctype_alpha($line)) {
fwrite(STDERR, "The information you entered contained more information that alphanumeric characters.\r\n");
} else {
fwrite(STDOUT, $line . "\r\n");
}
?>
[/php]
Arguments using argc and argv
Sometimes, you might have the need to create a shell script that will allow you to pass in the values at runtime. This simply happens using $argv and $argc. The count is contained in $argc. Note that there will always be 1 for the name of the script executed. Each argument is delimited by a space unless escaped or enclosed in quotes. So here is a simple example showing the input in a very simplistic way:

Retrieve Count and Print Out Variables

[php]
#!/path/to/php
< ?php
if ($argc > 0) {
fwrite(STDOUT, ‘# of arguments: ‘ . $argc . "\r\n");
foreach ($argv as $k => $v) {
fwrite(STDOUT, $k+1 . ‘: ‘ . $v . "\r\n");
}
}
?>
[/php]

Additional Resources
Nothing that was covered here was outside of the scope of the PHP manual. Look at the command line features in the PHP manual for items that I did not cover or for additional examples. In short, it is really easy to create PHP shell scripts that have many benefits and can be a great tool when used in the right context. Remember, use the best tool for the job. If you can do it, it doesn’t mean that you should.

form:http://blog.digitalstruct.com/2007/04/24/tips-and-tricks-php-shell-scripts/

发表评论