Sunday, March 5, 2006

It's All in the Shell

Shell scripts are supposed to make our lives easier, right? Scribble down a bit of bash magic and all your busy work will vanish faster than you can type #!/bin/bash. Too good to be true?

Well, perhaps, but it will make life much easier in the long run. As your scripts become more advanced, you'll spend less time doing boring repetitive tasks, and more time...eh, writing scripts.

The best way to start out is to just play around at the command line for a while. This will get you on your way, but here is a pair of tips that will help you train your scripting muscle:

  • Read a man page every day. The man pages are there to help you, but you can't take advantage of them unless you read them. You'll thank yourself later.

    Seriously. I'm not kidding. Do it for pleasure. Do it for power. Do it for the babes. Let's face it, ladies dig geeks, and this command that will lead you to everlasting glory:

    man $(ls -1 /usr/share/man/man1 | \
    sed -nr "$(($RANDOM % $(ls /usr/share/man/man1/ | wc -l) +1 ))p" | \
      sed "s/\.1\.gz//")

    What the heck is that, you ask? That will give you the low-down on a random command. Just think of it as "word of the day" for geeks.

  • Read other quality scripts. There are plenty of places you can look for them, but some good starting points are the scripts provided by your distribution (You are running an open source operating system, aren't you?). Try looking through the system startup scripts under /etc/init.d/. You can learn a lot of good tricks just by trying to follow along with the code.

0 comments: