Mar 202012
 

This is a very little-known feature of the Bash language that can be very useful when you have to knock up a script for someone else, particularly when they’re not Unix-savvy enough to be trusted with command line switches – give them a menu interface.

The select command is a bash internal (man bash) and it packs a lot into a single word. What it does is to allow you to create an ennumerated list that functions as an interactive text menu. All that’s required is to set the text of the prompt to the PS3 variable, and then pass a list of strings to the select command and a variable name. Whichever option from the list that the user chooses will then be assigned to that variable.

Example:

PS3="Choose (1-5):"
echo "Choose a name."
select name in tom dick harry barbara jerry
do
   break
done
echo "You chose $name."

Generates the output:

Choose a name.
1) tom
2) dick
3) harry
4) barbara
5) jerry
Choose (1-5):1
You chose tom.

The possibilities for creating idiot-proof, condescending, user-operated scripts are endless.


Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

Mar 192012
 

I’m really lazy, a quality that I firmly believe to be the hallmark of a good sysadmin. This is borne out by Larry Wall’s (Perl creator) “Virtues of a Programmer”.

That’s what I told them at my last exit interview, anyway.

So as part of this, you’ve got to learn how to crunch the bash command line as quickly as possible. It allows you to work faster, type less, and get back to doing something else. These are, I believe the ones I use the most, and which have become second nature.

Search history backwards Ctrl-R
Line kill Ctrl-U
Word kill Ctrl-W
Clear screen Ctrl-L
Clear to end of line Ctrl-K
Clear to end of word Ctrl-D
Yank back kill Ctrl-Y
Forward word Esc-F
Back word Esc-B

Use these and intimidate lesser sysadmins with your bash power.


Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

Mar 162012
 

When writing shell scripts, reuseability is important, and key to generalising the script is designing it to accept arguments. In order to remain consistent with the behaviour of other Unix commands – using switches and values – the easiest way to implement this without reinventing the wheel on every script is to use the “getopts” command.

Sadly, getopts seems to be one of those things which is easy to forget how to use, mostly because the syntax isn’t completely intuitive.

I like to have an example on hand to refer to, and plagiarise. Here is a simple one below. It accepts a toggle argument of “-v” to render the script verbose, and a switch “-f” which takes a filename as an argument – so pretty much your standard switches. Getopts parses this argument list and sets user variables accordingly. In order to understand the example below, all you need to know is that the colon (:) passed to “getopts” indicates that a string is expected as input. As you’d expect, any deviation from the pattern returns an error.

#!/usr/bin/sh

VFLAG=off
FILENAME=

while getopts vf: OPT
do
    case "$OPT" in
    v)       VFLAG=on;;
    f)         FILENAME="$OPTARG" ;;
    \?)        # Unknown flag
               echo >&; 2  "usage: $0 [ -v ] ... etc"
               exit 1;;
    esac
done

shift `expr $OPTIND-1`

echo $*            # remaining args in $*

So that’s it. Reuse getopts for clean, quick, professional and easily maintainable shell scripts.


Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.