ksh/bash "select" statement

Building a menu in a shell script is not an uncommon task, not difficult, but not particularly fun, either.

It turns out ksh and newer versions of bash support a statement, select, which makes simple menus a bit easier in some cases. Best to just dive in with a simple example.

PS3="What do you want to ride today? -> "
select MOTORCYCLE in FLTC K1200LT BuellBlast K100RT car; do
    if [[ $MOTORCYCLE == "car" ]]; then
        echo "you are no fun"
        break
    fi
    echo "Don't forget to gas up the $MOTORCYCLE"  
done
When run, gives you this:
1) FLTC
2) K1200LT
3) BuellBlast
4) K100RT
5) car
What do you want to ride today? -> 1
Don't forget to gas up the FLTC
What do you want to ride today? -> 2
Don't forget to gas up the K1200LT
What do you want to ride today? -> 3
Don't forget to gas up the BuellBlast
What do you want to ride today? -> 4
Don't forget to gas up the K100RT
What do you want to ride today? -> 5
you are no fun
$

 
 

Holland Consulting home page
Contact Holland Consulting
 

since Aug 3, 2024

Copyright 2024, Nick Holland, Holland Consulting