Tips and Tricks: PHP Shell Scripts
April 24, 2007
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:
#!/path/to/php < ?php fwrite(STDOUT, "Please enter in some input: "); $line = trim(fgets(STDIN)); fwrite(STDOUT, $line . "\r\n"); ?>
Retrieve Information, Check for Errors and Output:
#!/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"); } ?>
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
#!/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"); } } ?>
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.
What’s been going on…
April 23, 2007
This last year has gone by extremely fast, I am stating the last year because the last post on my blog was now slightly over a year ago. During this time many substantial changes have happened in my life that have caused my time to become extremely occupied leaving this blog untouched. I apologize to all of the people that have in the past, read my blog, or made comments regarding the topics that I may have posted on.
Never the less, in recent news, I am now in the middle of a job transition. I believe that with this transition it will help me to refocus on writing relevant information to you all. They say that when you make one change, it is easier to make several at once. Therefore, I am going to attempt to commit myself to a writing schedule where I will be posting a blog entry about some subject approximately once a week. It could be more, it could be less depending on the outlook of my schedule.
I look forward to posting new information here on my blog. As you can see I wanted to show that I was serious and spent a few hours redoing the design of the site. I hope the new color scheme is fitting and helps to engage you all! Regards.



