I get a little misty when I harken back to 1994 and my first linux install (slackware, kernel version 1.0).  The whole purpose of the excercise was so I could learn to write perl.  I had messed with Visual Basic and got frustrated with the runtime junk and the fact that extending it (OCX controls anyone?) was expensive.  Anyway, Perl became my wheelhouse for quite a long time.  I used to love the fact that you could do:

if ($haystack ~= "needle"){
  print "haystack contains a needle";
}

That’s the one thing I miss about Perl…the ~= or “yep, part of it matches”.  Anyway, the title of this one is Command-Line PHP, so let me get to it.

PHP is a lesser-known command line tool and more obiviously known as a language for web applications.  To me it’s the best of both worlds, primarily due to it’s simple system function calls and the inclusion of fancy-shmancy regular expressions.  Not to get all cosmic, but input is input and output is…yes…output. 

I recently completed a very large project that consisted of 18 command-line php scripts that did a WHOLE lot of file processing, database updates, file transfers, video transcoding, thumbnail creation, content auto-tagging and more.  Some of the files processed on a nightly basis are as large as 1GB, so uploading via a web form would be quite unweildy.  For example…I need to log every action I do with as many as 7 million transactions in a batch.  As long as you accommodate the processing time and memory requirements, it’s not an issue. 

Invocation is easy, and only requires the CLI version of php.  for example:

php /var/www/myFile.php

Another place I use command-line php extensively is to help debug php pages in web applications.  Makes it easy to find missing include files, debug function output, etc.  You can even run the page interactively with the -a switch

php -a /var/www/index.php

I highly suggest you consider php for your next command-line utility.  You’ll be happy with what is possible.  There is a great resource specific to command-line php located here: http://www.php-cli.com/