Snalk

Snalk » OS Tutorials » Unix » Creating Menu Interface

Reply
  #1 (permalink)  
Old 10-31-2008, 12:46 PM
Junior Member
 
Join Date: Oct 2008
Posts: 4
Post Creating Menu Interface

Creating Menu Interface



Creating a simple menu interface is easy. You can start with a configuration file that lists each choice on the menu and what command should be executed when that choice is selected:

|item=Change Your Password|cmd=/usr/bin/passwd
|item=Configure System|cmd=/usr/local/sbin/configure
|item=Restart DHCP Server|cmd=/usr/local/sbin/restart_dhcpd
|item=Reboot System|cmd=/usr/local/sbin/reboot


Once you have created the configuration file, you can use a script like the following to produce a menu interface:

#!/bin/bash

FILE='/usr/local/etc/mainmenu.conf'

get_value() {
echo "$1" | sed "s/.*|$2=\([^|]*\).*/\1/"
}

IFS=' '
select resp in \

'cat "$FILE" | while read line ; do
get_value "$line" item
done'
do
# Check for q or Q to exit
case "$REPLY" in
[qQ])
break
;;
esac
# Execute command
cat "$FILE" | while read line ; do
[ 'get_value "$line" item' == "$resp" ] && {
eval 'get_value "$line" cmd'
}
done
done


The select command does the hard part for you. You just have to tell it what options you want to display, and then take the appropriate action based on the option selected. Note that the special $IFS variable is set to the newline character to allow for spaces in the menu selections.

One way you can expand this script is to provide a method the user can use to enter parameters for certain commands. When an appropriate command is selected, the script could ask for the required parameters, and then execute the command as specified. This is something I would do in Perl, however, because it would be pretty complicated to parse such a complicated configuration file in a shell script.
Reply With Quote
Reply

Bookmarks

Tags
creating menu interface, interface, menu, menu interface, unix

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 01:11 PM.
Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.