
Scripting Language Shootout
|
227
• Shell scripts run slower.
• Shell syntax is icky.
Perl initially filled the gap as administrators looked for more productive tools, but
now PHP has migrated out of its web niche, and Python has gained a reputation for
productivity. We’ll write one application in each of these languages; several others,
such as Ruby and Tcl, are also available on Linux.
Our application will search the /etc/passwd file for name, user ID, hat size, or what-
ever else we can find in there. You’ll see how to open a file, read records, parse for-
mats, search for patterns, and print results. Then we’ll look at ways to avoid some of
this work, because sweat
!= productivity. You’ll be able to apply these techniques to
other files, such as logs or web pages. This is an example of data munging, and you’re
probably doing a lot of it already.
Let’s invent some requirements for our application and express them with this
pseudocode:
read a search string from the user
open the places file
for each line:
parse the fields (columns)
search the name field for a match
if there's a match:
print the other fields in a readable format
By now, many programmers would have rushed in and started typing (some without
having read the data format or requirements). Readers of this book are more disci-
plined, though, as well as better looking. They’ve had to fix the messes that the other
programmers have made and ...