Shell Scripting in PHP

Ever wish you had access to some nice string manipulation tools in a shell script?

Shell Scripting in PHP

Since I originally learned PHP (back when it was version 3) I always knew that you could enter an interactive mode of PHP, and even call php via the command line.  I never thought about it, as a web developer I rarely needed it (except a few odd cron jobs).  But recently I have come across some tasks that involved a lot of relatively complex and not so straight forward actions that were very difficult to accomplish via BASH.  (Yes it could be done... but how many awk | ed | grep * commands can one piggy back before one gets lost in a sea of ACK!!!!

This is when I remembered the little tidbit I had heard about being able to run PHP scripts directly from the command line even accepting arguments and controlling exit status codes.  This is not going to be an "end all say all" summary, but it should be enough for an experienced developer to take PHP to that "next" level.

So, IF you want to go to your prompt and execute a PHP script there are 2 ways I know to do it.  

1. Call the php binary and then reference your script:  

$bash: /usr/bin/php /home/matt/my_script.php [arguments here]

2. Add "#!/usr/bin/php" to the top of your script and call it just like you would a bash script.

$bash: ./my_script.php [arguments here]

So a sample "hello world" php shell script would look like this:

#!/usr/bin/php
<?php
echo 'Hello World!';

It is that simple.  The #! tells the shell what binary to use to interpret the script (just like #!/bin/bash would be used for a bash script). You must have the opening PHP tag because PHP expects it to tell it when to start interpreting.

You can use all of your PHP built in functions here... so far I have found very useful the FTP and imagemagic functions.  But also as a side note if there is something an external binary would do better... you can just call php's "exec" function and directly call it from your script.  Many (not all) of PHP's safety features are automatically disabled when you call it via command line.  For example the script TIME limit is no longer imposed. And quite a few other nice features.

I found that the php.ini script did still control memory usage... however you can set inline parameters to compensate.  For example, you can set your memory by using a ini_set('memory_limit', 512M); if you like in your script directly.

Why USE PHP as a shell script?

Well having experimented with quite a few languages, PHP is (in my opinion) one of the nicer general utility programing languages out there.  (Yes there are others...) But PHP is a very easy, loosely OOP, and has TONS of built in functions without having to include any external libraries.  I have found the file handling and networking utilizes INCREDIBLY useful in scripting.  So to answer the question why use PHP for shell scripting?  1. It is easy to use/learn. 2. It has a ton of built in libraries, and 3. It is relatively fast.  

I have also discovered you can interact nicely with databases via your shell scripts... ever need a shell script to access a full fledged database? Try making complex queries in BASH :)  

Closing

So all you web developers out there... you can actually write some very intense shell scripts without having to dust off BASH if you don't want too.  This is super handy for those odd CRON jobs that need to get done.  As a quick side note IF you are going to use this in a cronjob I recommend actually calling the php binary and referencing the script you want (/usr/bin/php /path/my_script.php) I seem to have much better luck with that.

For those of you who like a good programing read: http://www.php.net/manual/en/features.commandline.php

Written by Matthew Craig on Wednesday 23rd of November 2011 12:00:31 AM
Page Information:
  • Tags: Shell Script, PHP
  • Description: Writting shell scripts in php.