Muffinresearch Labs by Stuart Colville

Using ‘select’ for multiple choices in shell scripts | 1 Comment

Posted in Code on 8th November 2007, 1:03 am by

I found this neat construct in the gnu bash reference. Select is a great way to be able to present a list of options to a user in a shell script.

Take the example given in the reference:

select fname in *;
do
	echo you picked $fname \($REPLY\)
	break;
done

This outputs:

1) Applications                   6) Library                      11) User Guides And Information  16) bin                          21) mach_kernel                  26) tmp
2) Desktop DB                     7) Network                      12) Users                        17) dev                          22) mnt                          27) upnp.log
3) Desktop DF                     \8) Parallels                    13) VMware Images                18) etc                          23) private                      28) usr
4) Developer                      9) Public                       14) Volumes                      19) mach                         24) sbin                         29) var
5) Fonts Folder                  10) System                       15) automount                    20) mach.sym                     25) sw
#? 

Now if you type the number of one of the options e.g. 10 you’ll see the following:

you picked System (10)

As you can see in this example fname is set to the item in the list selected. The special var REPLY contains the number of the reply. Now to be able to use this in a script we would need to know when something other than one of the list items is selected. In this case the var fname will be set to null which can simply be tested with the -z switch in an if statement like so:

if [ -z $fname ]; then
    echo 'Your selection was invalid'
fi

To learn more about shell-scripting in general then I would certainly recommend checking out Classic Shell Scripting.

Post Tools

  • zagam

    Handy for selecting files, but not available in dash.
    Make sure such scripts start with “#!/bin/bash” or they
    will break when you update system shell to dash.

    #!/bin/sh
    # Not in ksh (available in both bash and dash)
    while :; do
    read -rp 'POSIX: Pick an option:--
    1) A
    2) B
    3) C
    #? ' REPLY
    case "$REPLY" in
    1) x=A ;;
    2) x=B ;;
    3) x=C ;;
    *) continue ;;
    esac
    echo "You picked $x (option $REPLY)."
    echo "Do $x."
    break
    done

GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(3)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

© Copyright 2004-12 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.498s.