More Tools For Building Tools

I’m working on more bash completions. This time for some of the command line tools Apple provides for sysadmins.

I decided there had to be a way to get a list of the options from the man page for a tool. After all they are all in there.

So I built a command line piece by piece. As an example let’s get a list of the options (with some caveats) for the tool pkgbuild. We start with man pkgbuild | col -b , the col -b step takes out the special characters man uses to show bold on screen. Now find all lines containing -- with grep, I liked grep -e '--'. If you have a look at the output of that we are getting close.

Next I decided to use sed to do a find and replace for the option itself. After some playing around I ended up with sed -e 's#.*\(--[a-zA-Z-]*\).*$#\1#' An important note for young players, it did take some time and a few tries to get that substitution just right. Don’t be afraid and remember Google (and Stack Exchange) are your friends.

First, I should point out an old Unix hand’s trick. Most of the time you see sed substitution commands using / as the separator but sed can use anything but \ or newline – it uses the first character it sees after the ‘s’. I usually use # as it makes the \ used for special characters easier to spot and the string easier to read.

Continue reading

Unix As A Second Language — Great New Blog

I’ve just discovered an excellent bog on IT World, a site I usually avoid as it is quite “corporate”. Unix As A Second Language by Sandra Henry-Stocker is, however an excellent source of Unix information and tips from anybody just starting out like her Unix Commands And Tools You Just Can’t Live Without to those on the edge of real expertise So You Want To Be A Unix Sysadmin and some for everyone Unix: Cheat Sheets — everything you wanted to know about everything … almost which pointed me to the excellent site OverAPI.

It hasn’t been there long but I suspect it’s going to be a great blog to follow for Unix users and those who delve into the command line of OS X.

Naming Windows and Tabs in Terminal

window_named

Would you like to name your Terminal window?

printf "e]2;`hostname -s`a"

That will turn it into your hostname (minus the domain). I’ve added the above to my standard .bashrc file on all the computers I visit via ssh.

If you want to do it yourself on the fly then

wname()
{
    printf "e]2;$*a"
}

added to your .bashrc file will allow you to say “wname The New Name” at the bash prompt any time you like. If you use tabs rather than multiple windows then:

tname()
{
    printf "e]1;$*a"
}

I’ll leave the function, which I define as ‘hname’, to set it back to the hostname as an exercise for the reader. Oh, and a final tip; you may notice that for the tab the string starts “e]1;” and for the window you replace the ‘1’ with a ‘2’. If you use ‘0’ then you change both the tab and the window name.

If you want to get really slick with your Terminal window name try setting it to the current directory. I now do this in my promptcommand like this:

PROMPT_COMMAND='history -a; history -n; printf "e]1;${PWD}a"'

Add that to your .profile. Now you have a directory path you can even command-click or click and drag on.

Setting up the command line

In a previous post I went through what I install on a fresh Mac. This time I want to go through how I set up bash and a few command line tools for use in Terminal.

I have to admit that I’m a command line nerd. I grew up professionally as a C programmer under Unix System V and back then the command line was all you had. I also do a lot of work on OS X Servers where you don’t want to install too many applications and the shell can be a godsend.

Continue reading

Terminal Power

A friend recently asked me for some tips on using the command line on the Mac and so I put together some notes. I’ve expanded them and posted them to my Google Site.

I give some tips on using the Terminal application, a couple of good ways to improve your bash sessions, a couple of handy ways of opening a man page and point towards a bunch of useful commands to have a look at.

I will be having a lok at some of my own scripts and seeing what I use most and explaining a few of those commands in a followup soon.