Many years ago Kevin Clark wrote a nice little menu generator for SCO systems. I've used this for years (Kevin and I were once partners in Lawrence and Clark, Inc.) and have always liked the simplicity of its design.
(Note: Kevin lost the source code for this. See Perl Kcmenu if you need this for something other than SCO Unix.)
Here's a simple menu:
# Comments: this is a menu $$My Menu $L$Run "ls"$ls -l | more $M$Read Mail$mail $Q$Quit$*exit*
If this is saved as "mymenu", then "kcmenu mymenu" will display a nice little boxed menu that probably works just as you expect it would.
Most of what you need to understand about kcmenu is explained in the sample menu included with the distribution and reproduced here:
# Kevin Clark's Menu System - kcm # (c) 1990-92 Kevin Clark # # sample menu for kcm use # # syntax is: # # # for remark # blank line print a blank line # first char token delimiter (!%^@~,`+=_*&$) # first token (max=6) short description - press 1st character # second token full description - centered if no 1st token # third token bourne shell function or *exit* macro # # kcm looks first in /usr/kctools or the directory pointed to # by environment variable KCMENUS then it looks in the current # directory for the menu file # # to always use present directory: # KCMENUS=. ; export KCMENUS # # execute with kcm sample.kcm # $$KCM Menu System $$Version 3.1 $$Copyright (c) 1990-92 by Kevin Clark %%This is a sample menu for the KCM Menu System. ||Use this as an example for designing your own menus !LIST!List files in current directory!l | more -w !COPY!Copy XENIX floppy disk!diskcp # remark lines do not add space to the menu # # Any delimiter is allowed: _DOS_List files on DOS floppy_dosdir A: | more -w ~FORMAT~Format floppy disk for XENIX~format /dev/rfd096ds15 &TAR&Copy present directory to floppy&tar cv2 . %QUIT%Quit Menu Program%*exit* !!************************************* !!** Thank you for using our product ** !!*************************************
Note that ANY delimiter is valid for separating menu descriptions from actions, and that each new line can have its own delimiter. The only special action is "*exit*", which exits the current menu.
You can nest menus by simply calling "kcmenu" with the name of another menu, and since the menus themselves are simply text files, you can build them "on the fly"; the following is a shell script that builds a menu that contains a list of files to be copied elswhere:
#!/bin/ksh menu=/tmp/m$$ export menu ( x=1 exec 1> $menu echo "" echo "!!Please choose:" echo "" for i in `ls -d [a-z]*` do echo "!$x!$i!cp $i /somewhere" x=$((x+1)) if [ $x -gt "15" ] then echo " " echo "!Q!Quit!*exit*" exit 0 fi done ) kcmenu /tmp/m$$
Obviously this is a simplistic example, but you can build fairly complex programs using this sort of technique.
Got something to add? Send me email.
More Articles by Tony Lawrence © 2012-07-14 Tony Lawrence
The idea of "work, then get paid" has been deeply ingrained in our culture by employers who want to limit their risk. Well, I like to limit my risks also. I like to get paid before I do work. (Tony Lawrence)