Creating a kornshell script with text-based menus in VI

Contributor Icon Contributed by seamonkey420 Date Icon August 13, 2004  
Tag Icon Tagged: Korn shell

This recipe give a quick run-through on how to create text based menus for ksh scripts in VI. My experience is with IBM AIX 4.x. This a very, very basic recipe.


Create a new file (for example, test.ksh)
by typing: vi test.ksh

the file test.ksh will be created and opened in VI.

in this file, we will add the following lines (i will explain each below)
________

clear
print “TEST Script MENU”
PS3=”Test Menu, enter choice:”
select clean_menu in “View script” “Edit script” “Print script” “Exit”
do
case $clean_menu in
“View script”)
pg test.ksh;;

“Edit script”)
vi test.ksh;;

“Print Report”)
lp test.ksh;;

“Exit”) break ;;
esac
done

_____

This will look like this when ran!

TEST Script MENU
1) View script
2) Edit script
3) Print script
4) Exit
Test Menu, enter choice:

this a super basic menu driven script.

_____

PS3= : what will show at the bottom, usually i have the name of the script (in my example, Test Menu, enter choice:)

select case_menu … : can be whatever you choose to be, just be sure you reference the same name in the: case $clean_menu in

The options after the select case_menu : they are the menu options that will show up. You need to have these match up with the references to them in the latter part of the script.

Always close each command with: ;; (you need 2!)

Best advice is to start with a very simple script and learn how that works. Then once you get the basics, you can start creating submenus, or have menu options that will call other scripts or menus or even have your script call functions you create with in the script.

Previous recipe | Next recipe |
 
blog comments powered by Disqus