Tuesday, April 1, 2008

Script For A Simple Menu Using Tput And ANSI Color

Simple Menu With An Option Selected

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 civis
2. 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 sc
4. Get back to (recover) the previously saved cursor position: tput rc
5. 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 ;)

Enjoy :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash
#
# SimpleMenu.sh - Only SomeWhat Useful ;)
#
# 2008 - Mike Golvach - eggi@comcast.net
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

trap 'tput reset;exit 1' 1 2 3 9 15

tput civis

while :
do
echo -e "\033[44;37;22m"
clear
echo -e "\033[41;37"

echo -ne "\033[46;30m"
tput cup 8 25 ; echo -n "The Linux and Unix Menagerie"
tput cup 9 25 ; echo -n " Shell Account Services "
tput cup 10 25 ; echo -n " W E L C O M E "
echo -e "\033[40;32m"

tput cup 12 25 ; echo -n " 1. See Who's Logged In "
tput cup 13 25 ; echo -n " 2. Reboot The Server "
tput cup 14 25 ; echo -n " 3. Gain Root Access "
tput cup 15 25 ; echo -n " 4. Call Your Mother "
tput cup 16 25 ; echo -n " 5. Quit "

echo -ne "\033[41;30m"
tput cup 18 28 ; echo -n " Pick Your Poison: "
tput cup 18 48
tput sc

read y
tput cup 20 30

case "$y" in
1|S|s)
output=`w|awk '{print $1}' |xargs -ibing grep bing /etc/passwd|awk -F":" '{print $5}'`
starting_line=21
tput cup $starting_line 28;
echo -ne "\033[46;30mCurrently Logged In Users:"
echo -ne "\033[0m"
echo "$output"|while read x
do
let starting_line=$starting_line+1
tput cup $starting_line 28;
echo -n $x
done
tput rc
echo -n "Any Key To Menu"
read x
;;
2|R|r)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
3|G|g)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
4|C|c)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
5|Q|q)
tput reset
clear
exit
;;
*)
tput cup 20 28
echo "$y - Unknown Option"
tput rc
echo -n "Any Key To Menu"
read x
;;
esac
done

, Mike