Tuesday, April 1, 2008

Click above to see a larger example of what happens when you dare to select option 1 ;)

Hey there,

Today I thought we'd look at something that almost all admins have to do at some point or another; create a simple shell menu script for users who aren't Unix or Linux savvy, but need to make use of the systems directly, nonetheless.

To spice it up (a little. There's only so much you can do ;) I've used colors within the standard shell menu framework, sticking with the ECMA compliant ANSI color codes. The reason I choose to stand by these color codes (since they, admittedly, lack the range of colors available in different flavors of Linux and Unix) is that they're virtually guaranteed to work on "all" flavors of Linux and Unix. Or maybe I'm just a people pleaser who hasn't realized that he can't win 'em all yet ;)

You'll notice also that, rather than just throw the menu together, using "case" inside a "select" loop, I went out of my way to complicate things by using tput to manage the cursor on the screen. In this case, it actually adds some functionality to the menu that would, otherwise, be impossible to implement. I shied away from unnecessarily using "getopts" when our "case" statement can handle these simple menu arguments just as efficiently :).

You can run this menu very simply, from the command line, like so:

host # ./SimpleMenu.sh

And there are only a few things to really take note of today. In a future post, I'll devote more time to utilizing your terminal screen with tput. It can be used to do a lot more useful, and entertaining, things than what we're having it do today.

Basically, in quick sequential order, we're using tput to:

1. Make the cursor invisible : tput civis2. Position the cursor at various places on the terminal: tput cup x y <--- With x and y being single numeric coordinates designating the row and column, respectively.3. Save the current cursor position: tput sc4. Get back to (recover) the previously saved cursor position: tput rc5. Make everything go back to the way it was, just in case our screen gets screwed up: tput reset

You can also, very simply, modify this shell menu script by just changing the menu options and adding your own routines to the case loop. And, if you can find something less offensive to your sensibilities, please do change the color scheme ;)