Wicked Cool Shell Scripts
James Gaskin spoke with Dave Taylor about his new book, Wicked Cool Shell Scripts. Following is an edited transcript of that conversation, or listen to it here.
Welcome to ITworld of Voices. My name is James Gaskin and I'll be your host for this series of interviews with the leading technology book authors of today. Our guest in this session is Dave Taylor. Dave has contributed to a variety of Unix and Internet projects starting in 1980, before there was technically an Internet. He wrote Elm, the first screen-oriented email application, and has also foisted 16 books upon us, covering various technology and business projects and subjects. He was actually born in London and got to the United States as soon as possible. Two of the books you may have heard of from Dave are Teach Yourself Unix in 24 Hours and Creating Cool Web Pages With HTML. Our book subject today is Wicked Cool Shell Scripts. It came out in mid 2004, published by No Starch Press. It won, among other things the LinuxWorld Magazine's award for Best Scripting Book Ever. Dave, thanks for taking some time for us today.
Dave Taylor: I'm glad to be involved James.
James Gaskin: Thank you. First question, why would anybody write a book about shells?
Taylor: Ah, well, that's a good question. Because as you might realize, there are plenty of books that cover shell scripts. And shell scripts are basically small little portable applications that you write within the command line interpreter on Unix or Linux machines, or in fact, Mac OS X machines too. And there are plenty of them out there, but when I looked at them all, I kept having this niggling unhappiness that they were all terribly boring and that it was a very interesting topic that no one had ever really addressed in an interesting fashion, and that they were much more about here's an if statement. Here's how you use an if statement. Let's have an example three-line snippet that has an if statement in it. Terribly dry. I looked at that and I said, there has got to be a better way, and so I wrote Wicked Cool Shell Scripts, which has 101 -- actually probably about 160 -- different scripts that cover much more than you could ever imagine you could do with a shell script. All the way from a hangman game to CGI scripts you can plug into your server to a wide variety of tools that can help you administer your Unix or Linux machine, to even ways that you can work with the iTunes library on a Macintosh.
Gaskin: Interesting. Now, desktop Linux has certainly become more popular. Linux has done very well in the server room. But most people are scared of the command line. So what advice could you give them?
Taylor: Well, the first thing I would say is to go and crack open a book like, if I may say so, Teach Yourself Unix in 24 Hours, and experiment. The good news is that it's really pretty hard to damage anything. As long as your intent is to look at things and to create new things, rather than to delete things or edit existing configuration files that you don't understand, then you're in good shape and there's very little trouble that you can cause. What you will find is, in a very short amount of time, you will realize that there is tremendous power to typing things in rather than using a graphical interface.
Gaskin: You said that this book is meant to be non-boring compared to some of the other shell books. What have you done particularly to make it more entertaining?
Taylor: Well, the first thing is is I don't tell you how to write a shell script. I think that there are, as we've already discussed, plenty of books about how to write a shell script. So I just assume you can pick one of those up or you can ask some people and so right off the bat, the very first script I present within the first four or five pages is what other books had in their appendix as a super-advanced topic. I explain everything and I talk through the logic of what's happening and I highlight particular commands and how they're being used, but I don't waste time talking about what an if statement and how a semicolon works. So the first thing that makes this book interesting is that it goes right into the meat of things. The second thing is that I really tried to cast as broad a net as possible. So I have some examples that you wouldn't think of traditionally having as a shell script. For example, I have a pair of scripts that let you actually track your entire stock portfolio, using the latest stock quotes from Yahoo Finances. Every morning it can email you what your net worth is and how it changed from the previous day. Now, you might actually want to only have it tell you when you've improved your net worth so that you don't go on a suicidal deletion binge or something, but all of those capabilities are there and it's an easily produced shell script. And it's certainly highly interesting to read. People that are using any administrative shell scripts at all are used to these long tedious scripts that do very boring things. So having the opportunity to look at this from a very different perspective, I think really can expand their horizons too.
Gaskin: Do you have a favorite script in the book?
Taylor: Well, my immediate reaction to that is my hangman game. It is one of my favorite, mostly because it took me an amazing amount of time to figure out how to write hangman in 50 lines. And it's 50 shell script lines that are actually well commented. So I did finally figure it out and it took a lot of thinking about things the opposite to how you would expect and I think it's a very informative script to read. In terms of other scripts, oh, I don't know. There's a script I have that helps you manage the password file on an Apache Web server, which I think proves to be very interesting and it actually has a graphical Web-based interface. I would say that those are probably the two biggest ones. There's also one that lets you explore, like I said, your iTunes library, which proves to be very interesting. And there's a variety of scripts that let you automatically synchronize local and remote directories using either FTP or secure FTP.
Gaskin: Oh, that would be a marvelous tool for administrators trying to protect the data of the users who don't know what backup means.
Taylor: Right, and you could even have it where you just tell the user, whenever you want to, type in this word on the command line and everything will magically be downloaded to your machine, only what's changed will actually be downloaded and so it will be fast and efficient and you'll always have your own copy.
Gaskin: Oh, that sounds like that one script might be worth the cost of the book for many administrators if they have the kind of users I'm familiar with.
Taylor: Well, I'd have to agree with that statement.
Gaskin: There's quite a bit of choices for the type of Unix and type of Linux operating system you want to use. Are these scripts somewhat portable or very portable across the various flavors?
Taylor: They're extremely portable. They've all been tested on Linux and Unix and Mac OS X machines. And honestly, the only system that I have ever tested these on that has any problems is, and I'm sorry to say this for the people that like Sun Microsystems, is Solaris. It's detailed in the book, and it's also discussed on the book's website which is at Intuitive.com/wicked.
Gaskin: Is there anything you can't do with the shell script?
Taylor: That's a tough one, particularly since they now have with different operating environments the ability to popup dialogue windows and other graphical user interface elements from within a shell script. So I would say that for fairly lightweight tasks, there's very little you can't do with a shell script. I could, for example, do a flat file database all as shell scripts. Because when you think about it, a shell script is really just a way for you to put together all the power of the thousands of different Unix commands in a coherent fashion. Anything you can do on the command line, you can do in a shell script in an automated fashion. When you're ready to crack open the hood and soup up your engine a little bit, then there's a tremendous amount of power underneath there.
Gaskin: Well, that's wonderful. And the book again is Wicked Cool Shell Scripts, written by Dave Taylor, one of his 16 books. Thanks for your help Dave.
This has been Dave Taylor speaking about his book, Wicked Cool Shell Scripts from No Starch Press. This is ITworld Voices. I'm James Gaskin and thanks for listening.