Scripting Simple Shell Commands
I'm setting up a latop with a fresh install of Ubuntu. I haven't done a clean install on a real machine in who knows when, and over the last 3 months or so I've become a huge fan of PPAs. I have several PPAs to keep up to date with packages I use that have frequent updates beyond what's available from Ubuntu updates. Since I'm adding several at once, I'm adding a lot of PPA keys. Doing this reminded me of a simple trick I use when running a frequent command. (Some form of this is common among developers or sys admins, but if you're less experienced with a shell or Linux this may be useful to know.)
First, the command for installing a PPA key is...
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com [KEY]
...where [KEY] is the PPA's key finger print.
So the easiest way to use this is to cycle back through your bash
history and replace the previous run with the new key and run again.
For me, I know I won't remember the particulars of this command in a
month or two when I need to run it again and it's no longer showing up
in my bash history. I'll end up having to consult a man page or the
Launchpad help pages. Not a
big deal, but with things like this -- commands I use semi-regularly
but not enough to keep in memory -- I usually add a little bash
script. For example, with this command I created a script called
add-ppa-key:
#!/bin/bash if [ $# -eq 0 ]; then echo "Usage: add-ppa-key KEY_FINGERPRINT" exit 1 else FINGER=$1 fi sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com $FINGER
I always add the usage test with each command like this, just so I
can use tab completion to find the command, run it with no arguments,
and get back a statement of what I need to do. I have a "bin"
directory in my home directory and append $HOME/bin
to $PATH in my .bashrc. I have several of these type of
scripts in this bin directory, for building software or even for simple
to remember but frequently used rsync commands or PythonPath export
statements. Not only does this make commands reusable without having
to completely remember them, it's also nice for documenting commands.
So the next time someone asks me what the command for installing a PPA
key is, I can do cat ~/bin/add-ppa-key and paste the
output into IRC, IM, or what have you.
Posted by deryck on April 1, 2009

