Mac in The Shell: Scripting with PHP
Volume Number: 24 (2008)
Issue Number: 03
Column Tag: Mac in The Shell:
Mac in The Shell: Scripting with PHP
Forget that web stuff, PHP is a great scripting language!
by Edward Marczak
Introduction
Since the inception of this column, I've covered general shell tools, bash commands and general bash scripting. Life in the shell involves much more! There are many other scripting environments - and shells, for that matter! While I won't be stepping totally outside of bash, I will be looking at other ways to automate your environment. While I like bash, I don't necessarily love it. PHP - yes, the one you may know from web development - has a CLI component that makes a great scripting language. Of course, it's installed by default under OS X (version 5.2.4 as of OS 10.5.1). So let's dig in and learn some new techniques.
Why, oh Why?
Why am I forsaking bash? While bash is powerful, it falls down in some key areas, one of which being easy database access. Yes, using the tools we've talked about over the months, we could use the mysql binary, send output to standard out, grep-sed-and-awk our way to get what we're after. But that's not easy. Or elegant.
PHP is a dynamically and loosely typed language with extensions that allow easy access to data outside of its world. PHP supports access to raw network sockets, curl libraries allow access to URLs (ftp, http, https, etc.), and various database libraries allow access to various databases. This support needs to be compiled in, and Apple has made sure we have the tools we need. (You can look at everything that's compiled in by running php -r 'phpinfo();' | less. Once you're looking at that output, search for 'curl' and 'mysql').
PHP is also Open Source, like many of the packages that Apple includes with OS X. It was written by Rasmus Lerdorf in 1995 (as "PHP/FI" which evolved into the PHP we use today), and stands for "PHP: Hypertext Preprocessor". While its goal was for web development, it's also now nicely suited for scripting. (There wasn't always a CLI component).
What can PHP do? Just about anything! Well, anything that you'd use a scripting language for. PHP runs on just about every platform, and there is even a way to tie in a GUI (but that's beyond what I'm going to go into here. See http://gtk.php.net/ for more information).
Do the PHP
Nothing like an example to get things going. Most text editors recognize the .php filename extension and change modes accordingly. TextWrangler, BBEdit, vi and TextMate all have colorizations and in some cases, extra support for the PHP language and its constructs. So, fire up your favorite one, and type the venerable "hello world" program:
Listing 1: hello_world.php
#!/usr/bin/php
<?php
print "Hello, world!\n";
?>
As with all programs that we're going to run from the shell, we need to mark it executable if we intend to run it by name alone (chmod 770 hello_world.php). Notice that I used the shebang line here (line 1). Technically, you could omit that, and run the program like this:
php hello_world.php
However, I think the shebang line along with marking it executable underscores that we can treat PHP like a 'real' scripting language. This way, we can simply:
./hello_world.php
and be on our way. Let's look closer at listing 1.
First, we have the shebang line as discussed. Next, we have this odd-looking construct: "<?php". This simply signifies that what follows will be PHP code. As originally intended, PHP can be embedded in HTML documents. The PHP opening ("<?php") and closing ("?>") tags allow you to jump in and out of "PHP mode." (As an aside, if you're not in "PHP mode," you're in HTML mode, and the php engine will happily spit stray lines back to stdout. That's why there is no blank line in listing 1 between the shebang and the php opening tag. Also, the final closing tag, technically, is optional).
The next line contains a print statement, which simply outputs its arguments to standard out. In this case, the argument is the string "Hello, world!\n". The "\n" character is a newline, which print does not output on its own after printing.
Finally, we close with an ending php tag: "?>". All in all, you probably figured out this short program without explanation. One thing to note, though: unlike the bash constructs that we've seen, PHP expects each line to end with a semi-colon (";"). PHP uses this as a line separator, and the PHP engine will print errors if you omit it at the end of any line or between any separate statements.
Beyond The Beginning
Like any good programming environment, PHP supports comments, variables and all basic constructs such as flow control statements and loops. Let's get the easy one out of the way: good code is commented code.
PHP honors C, C++ and Perl style comments. Let's look at all three in action.
Listing 2: Comment example
<?php
/* example1.php
This short code snippet illustrates good code commenting.
Ed Marczak, 2008
*/
$numargs = func_num_args(); // This retrieves the number of args passed
echo "Number of arguments: $numargs\n";
# Here's the closing tag:
?>
I'm sure I don't need to belabor that any further.
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. A valid variable name starts with a letter or underscore (not a number) and is then followed by any number of letters, numbers or underscores. (With one exception: "$this" is a reserved variable name and cannot be overridden). As mentioned, PHP is both loosely and dynamically typed. This is a fancy way of saying variables take on the properties of the contents assigned to them, and generally 'do the right thing.' As always, an example:
Listing 3: PHP Variable Demo
<?php
$a = "apple"; // a is a string
if ($a=="orange") print "Orange\n";
$a=5 // a is now an integer
?>
(Note: the final closing tag can also act as a semi-colon, hence its omission in listing 3). In general, this aids the rapid development that can be done with PHP. However, if you ever do need to find out what type a variable is at the moment, you have several options. You can print it out using getype() or you can test for it with istype(). You can also use "===" in comparisons to ensure not only that variable contents are the same, but that types match also. You can force a variable to be a certain type by using settype(), or by casting the variable if you're familiar with that from other languages. I won't delve into this much deeper except to say to be careful with this. Again, the PHP engine will try to 'do the right thing.' Take listing 4 as your warning.
Listing 4: Casting example
<?php
$x=TRUE; // x is a boolean
$x=(string)$x; // x now contains "1"
$x="9bar"; // string again
settype($x, integer); // x is now an int, and 0
?>
In a final show of the PHP engine trying to do the right thing, it will "juggle types" as needed. So, if $a is an integer and $b is a float, adding them together evaluates everything as a float, and returns a float. Strings also gain an implicit conversion if used with mathematical operators. "15" + 5 equals integer 20.
Speaking of types, PHP supports the following types:
- Integer
- Boolean
- Float (aka "double" or "real")
- String
- Binary
- Array
- Object
We'll be exploring these types as we go.
Second Gear
Other things to note about how PHP deals with string variables and quoting. String literals can be specified as single quoted, double quoted or use heredoc syntax. I'll concentrate on the first two for now.
With single quotes, variables are not expanded, and only a backslash need be escaped. You can nest double quotes within single quotes. For example:
print 'Monty Python\'s Flying Circus.';
print 'I like $dollars';
print 'I\'m splitting
this line';
The first line prints "Monty Python's Flying Circus." The second literally prints "I like $dollars." - without trying to evaluate "$dollars" as a variable. The third example shows that we can even embed the newline character into a single-quoted string.
If a string is enclosed in double quotes, PHP expands variables and interprets certain escape sequences. The major ones are:
- \n linefeed (0x0A, or 10 in ASCII)
- \r Carriage return (0x0D, or 13 in ASCII)
- \t Horizontal tab (HT or 0x09 (9) in ASCII)
- \v Vertical tab
- \f Form feed (since PHP 5.2.5)
- \\ Backslash
- \$ Dollar sign
- \" Double quote
In action:
$num_dogs=6;
print "There are $num_dogs dogs\n";
This prints "There are 6 dogs" followed by a newline character.
Flow Control
All flow control deals with comparison for purposes of directing flow or knowing how many times to loop. PHP understands the following flow control comparison operators:
- == Equal
- === Identical
- != Not equal
- <> Not equal
- !== Not identical
- < Less than
- > Greater than
- <= Less than or equal to
- >= Greater than or equal to
Let's use these in a simple example:
Listing 5: Basic Flow Control
if ($a > $b) {
print "a is greater than b\n";
$top = $a;
} else {
print "b is greater than a";
$top = $b;
}
PHP uses curly brace syntax to create a statement group. Listing 5 illustrates an if flow control structure. Generically, if tests an expression - any expression. If a is greater than b, the flow follows into the first group if statements. Otherwise, we run the statements in the else group.
Control structures can also be nested.
Listing 6: Nested control structures
$i=100;
while ($i<=500) {
if (fmod($i,2)==0)
print "$i\n";
$i++;
}
You may note from listing 6 that if a control structure only has a single statement, curly braces can be omitted (though I recommend always retaining the braces).
Shell Interaction
There are a few ways that you can have your PHP script interact with the shell in general. First, you can have PHP execute other shell commands. Second, you can pass arguments in to the script from the command line. Third, PHP is fully capable of reading from standard in and directing output to standard out and standard error.
The first one is pretty easy: to run a shell command - one that may have no built-in PHP equivalent - you simply use the backtick operator. The output of the shell command is assigned to the variable of your choice. Listing 7 shows this in action.
Listing 7: diskmon.php
#!/usr/bin/php
<?php
$freespace=trim(`df / | tail -1 | awk '{print $5}' | cut -d "%" -f1`);
if ($freespace > 80) {
print "System volume is at ${freespace}% full - you may want to look at that.\n";
die();
}
if ($freespace > 50) {
print "System volume is at ${freespace}% full - seems OK.\n";
die();
}
if ($freespace >= 0) {
print "System volume is only ${freespace}% full - no problems.\n";
die();
}
print "System volume is an indeterminate amount full.\n";
Accepting and handling command line arguments is also a straight-forward venture. PHP populates the variables $argc and $argv (an array) with the count of arguments and the contents of the arguments respectively. A simple example:
if ($argc < 3) { // we need 2 actual arguments
print "Usage: $argv[0] param1 param2";
die();
}
print "You entered $argv[1] and $argv[2].\n";
The first element of $argv (which starts counting from zero) will contain the name of the program being run, as called from the command line. So, if someone symlinks to your program and it is called that way, $argv[0] will contain the name of the symlink.
Finally, PHP can easily handle something like this:
$ codeprep | php > accounting.csv
...where the codeprep application is actually outputting php code. Naturally, PHP will read standard in like so:
$ ls -l | list_filter.php
The program list_filter.php would contain a loop like this:
while ($line = trim(fgets(STDIN))) {
// Process input here
print "$line\n";
}
Standard out is standard out: all echo and print statements are sent there automatically. But what if you want to 'do the right thing' and send error output via standard error? Easy: just use fwrite to direct output to that stream:
fwrite (STDERR, "Record number $rec_no is malformed\n");
With this in your code, you can still do this:
$ data_gen.php > datafile.csv
Record number 70 is malformed
Record number 103 is malformed
You still end up with a good data file, but also can be alerted to exceptions.
Conclusion
PHP is just one of the many nice ways to get into, or continue scripting under OS X. The brilliant thing is that OS X treats all scripting languages pretty equally. Additionally, if you're already familiar with PHP from web development, it makes a nice and easy transition into scripting for the system environment. PHP-based scripts can be used for anything that bash or perl scripts are: triggered automation from cron, GUI interaction and even our beloved login hooks.
Next month, we'll dip further into PHP, interaction with MySQL and other PHP-based script topics.
Media of the month: The Illustrated World's Religions: A Guide to Our Wisdom Traditions by Huston Smith. This one has been around for a bit, but its easy reading overviews and beautiful photography make this a good general read and nice reference guide. Once you see the different perspectives in this book, perhaps we end the emacs / vi wars!
Until next month, keep scripting.
Ed Marczak is the Executive Editor for MacTech Magazine, and has been lucky enough to have ridden the computing and technology wave from early on. From teletype computing to MVS to Netware to modern OS X, his interest was piqued. He has also been fortunate enough to come into contact with some of the best minds in the business. Ed spends his non-compute time with his wife and two daughters.