PHP and Shell Scripting: Using Pipes

Following Darrell Brogdon's previous article on using PHP as a shell scripting language, this article covers some advanced uses of the language and shows some tricks that make PHP an extremely useful language to work with.

Unlike Darrel's article however, this one will also cover the Windows version of PHP as well as the Linux/Unix one. First, however, there are a couple of differences you need to be aware of.

So what are the differences?

The differences between the two environments are mostly due to operating system differences as opposed to differences in PHP itself. The big one has to be case sensitivity in file names; this has nailed me on many an occasions. A very good habit to get into to avoid this, is to always use lowercase filenames no matter what. How you do it is up to you, but lowercase filenames have always worked for me. The other thing you need to be very aware of are the differences in the file system.

Windows uses a system of drive letters and separate disks, where as most posix based systems (this is the technical name for Linux and Unix variants) treat all file systems and disks as one large directory tree. If you're only working in one folder this should not present a problem, but if youre working with globs and other file system functions, you need to be aware of this.

The 3rd most important thing is one of binary versus text when writing files. Under Linux/Unix, all files are handled identically and that is as a byte stream, whereas under Windows the situation is a little different. Windows treats text files as a separate type than binary files. Thankfully in the latest builds of PHP this is no longer an issue to be concerned with as the fopen calls now default to binary mode.

The last thing to be aware of is line endings. Under Windows these take the form \r\n as opposed to just \n on Linux/Unix.

So to sum up, watch out for position in the file system, check your filenames and be cautious of line endings.

Why are these differences important in a Shell environment?

First and foremost, the case situation will manifest itself when running scripts. A script named 'MyScript.php' will happily run from the command prompt using 'PHP -q myscript.php' on Windows, but will fail miserably on Linux. File system position will show up when using glob functions, and other directory scanning functions. Lastly the line ending issue is most likely to surprise you if your parsing files, text streams or the output from commands. From this point on, Ill not mention these points again, but if something is not working as you expect, then keep them in mind.

So what neat things can we do with PHP in the shell?

Well, the answer to that is pretty much anything. I use PHP for a huge amount of different tasks, from dumping files to chucking together quick test models or proof of concept ideas. Over the years however, one of the great things Ive found with PHP is its ability to hack together quick pipe filters.

For those of you who are not up to speed on pipe filters, a quick explanation is in order. When the predecessors of todays modern OS's where developed back in the 60's the idea of one tool, many uses was a very common one. Shell programming spawned a whole generation of people who spent insane amounts of time writing ridiculously long command lines, to perform some quite long tasks. Its for this very reason that today most OS's (Windows included) have a very rich set of separate programs that each do a small but efficient task.