|
|||
|
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. |
![]() |
| Bookmarks |
| Tags |
| creating menu interface, interface, menu, menu interface, unix |
| Thread Tools | |
| Display Modes | |
|
|