Muffinresearch Labs by Stuart Colville

Using ’select’ for multiple choices in shell scripts | Comments (0)

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

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

Comments: Add yours







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

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