Skip to content

Musings of an Anonymous Geek

Made with only the finest 1's and 0's

Menu
  • About
  • Search Results
Menu

A Missing Battery in Python: a bash “select” equivalent

Posted on June 3, 2009June 11, 2009 by bkjones

So, someone showed me a script they were writing in bash. They were doing a lot of manual menu creation, then using “read” to collect input, and manually mapping the input to longer strings, and then doing string manipulation stuff. Trying to help simplify and make things a little more sane and readable, I discovered that I was going to break some of my own rules with regards to bash scripting:

  1. It was likely going to involve arrays, which is usually the time I migrate to Perl or Python.
  2. It was going to be longer than 60 lines, which is usually when I migrate to Perl or Python.
  3. The script didn’t make calls to any standard system commands, which means bash was being used for its generic scripting structures like case statements and if-test checks, which is something I try to foresee when I write scripts. If I’m not greatly aided by standard CLI utilities, I try not to do it in bash, because in the end, it’s a shell, not a scripting language.
  4. There was no requirement to use bash, and Python and Perl are both available.
  5. XML was involved.

The first order of business, though, was to replace all of the menu-related stuff with “select” loops instead of doing all of the manual stuff. It made things a lot simpler. Once that was out of the way, I inspected more of the code, and at that time realized “oh wait a minute….”, and became slightly ill as the above menu items flashed into my brain.

So I set about the task of fooling around with the task in Python. My first discovery there was that there isn’t any “select”-like functionality built into Python. Then I tried the curses module, but that makes doing “select”-like menus even messier, because not only does it *not* store the *string* associated with an option, it doesn’t even store the option string directly — it stores the ASCII code. This makes curses a lot more robust than using bash, but between messing with screen, window, and box semantics, and then still having to do some kind of option-to-value mapping by hand, it’s not much of a win in the simplicity department.

There are a couple of third-party modules that deal specifically with text-based, menu-driven, non-curses apps, so solutions do exist (this one looks good at first glance), but it requires altering a stock, rpm-based Python installation, which isn’t so bad except that it also means maintaining that alteration going forward — something I’d like to avoid.

What ‘select’ Actually Does in Bash

Here’s a bit from the bash reference manual, which is way more concise than I’m ever likely to be:

select
The select construct allows the easy generation of menus. It has almost the same syntax as the for command:

          select name [in words ...]; do commands; done

The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error output stream, each preceded by a number. If the ‘in words’ is omitted, the positional parameters are printed, as if ‘in “$@”’ had been specified. The PS3 prompt is then displayed and a line is read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the select command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY.

The commands are executed after each selection until a break command is executed, at which point the select command completes.

Here is an example that allows the user to pick a filename from the current directory, and displays the name and index of the file selected.

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

I’ll figure out a solution that isn’t too ugly and post it here. In the meantime, if you’ve already done all of that, or I missed something in the docs for Python or curses, post links or code in the comments. Thanks!

PS – Before I get flamed: I chose Python because it’s my go-to language for all of my scripting tasks. I don’t recall Perl having an easy way to do this either without also installing an external module, and I figure I’ll learn something new about my favorite language while trying to solve this problem.

[UPDATE] – As soon as I launched the Python interpreter and started thinking along the lines of Python dictionaries, things fell into place:

>>> d = {1:”foo”, 2:”bar”, 3:”baz”}
>>> d
{1: ‘foo’, 2: ‘bar’, 3: ‘baz’}
>>> for k,v in d.iteritems():
…     print k,v
…
1 foo
2 bar
3 baz
>>> d.get(1)
‘foo’

That works, is still much cleaner than bash “read”, and gets me the option-to-value mapping, without loading *any* modules.

[UPDATE 2: It was pointed out that I failed to explain what ‘select’ actually does, so I’ve added a description from the bash documentation, which I think explains it pretty clearly. ]

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Facebook (Opens in new window) Facebook

Recent Posts

  • Auditing Your Data Migration To ClickHouse Using ClickHouse Local
  • ClickHouse Cheat Sheet 2024
  • User Activation With Django and Djoser
  • Python Selenium Webdriver Notes
  • On Keeping A Journal and Journaling
  • What Geeks Could Learn From Working In Restaurants
  • What I’ve Been Up To
  • PyCon Talk Proposals: All You Need to Know And More
  • Sending Alerts With Graphite Graphs From Nagios
  • The Python User Group in Princeton (PUG-IP): 6 months in

Categories

  • Apple
  • Big Ideas
  • Books
  • CodeKata
  • Database
  • Django
  • Freelancing
  • Hacks
  • journaling
  • Leadership
  • Linux
  • LinuxLaboratory
  • Loghetti
  • Me stuff
  • Other Cool Blogs
  • PHP
  • Productivity
  • Python
  • PyTPMOTW
  • Ruby
  • Scripting
  • Sysadmin
  • Technology
  • Testing
  • Uncategorized
  • Web Services
  • Woodworking

Archives

  • January 2024
  • May 2021
  • December 2020
  • January 2014
  • September 2012
  • August 2012
  • February 2012
  • November 2011
  • October 2011
  • June 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • September 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
© 2025 Musings of an Anonymous Geek | Powered by Minimalist Blog WordPress Theme