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

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:

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 *;
          	echo you picked $fname \($REPLY\)

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)

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. ]

  • http://www.doughellmann.com/ Doug Hellmann

    Don’t depend on d.iteritems() returning them in sorted (or even consistent) order. If you’re using numerical indexes you can use enumerate() with a list of actions, otherwise wrap d.iteritems() with sorted().

  • m0j0

    Thanks a million, Doug. Updating code now.

  • http://paddy3118.blogspot.com Paddy3118

    Your post inspired me to write this new task on Rosetta Code.

  • m0j0

    Nice. Do one for Perl’s Tie::File functionality too! ;-P

  • http://mathieu.agopian.info Mathieu Agopian

    You could go, as Doug said, with enumerate and a tuple:

    >>> d = (“foo”, “bar”, “baz”)
    >>> d
    (‘foo’, ‘bar’, ‘baz’)
    >>> for i, v in enumerate(d):
    … print i + 1, v

    1 foo
    2 bar
    3 baz
    >>> d[2 – 1] # -1 because enumerate is 0-indexed and we printed 1-indexed

  • http://www.ironpythoninaction.com Michael Foord

    This blog entry would have been greatly improved by explaining the semantics of what select does. 🙂

  • m0j0


    Thanks for pointing out that glaring omission, Michael.

  • Pingback: Twitter Trackbacks for A Missing Battery in Python: a bash “select” equivalent | Musings of an Anonymous Geek [protocolostomy.com] on Topsy.com

  • http://catherinedevlin.blogspot.com Catherine Devlin

    Hmm… wonder if I can fit a SELECT into cmd2 in time for PyCon. (Granted, that still wouldn’t make it an included battery.)