Menu driven using Ksh

Hi Guys,

I would like to know how to write Menu driven programs
using ksh.

I have several script files 1.sh 2.sh 3.sh ...so on 25 files
I want to create a Menu which will calls submenus.

Main Menu

  1. Data Entry

      if you press 1 again submenu  1. Order entry
                                                       2. Customer Entry
                                                       3. Exit
       if I Press Escape here it should go back to main menu
       ctrl key to be trapped
    
  2. Reports

  3. Queries

  4. Exit

If any one know it please mail me to:

::email removed::

Thanks & Regards
Krishna

Create a function for each menu option. Read the user input, and map the number to the function using a case statement. Repeat as necessary.

something like this script below, it is written for MKS toolkit to be run on windows but it's based on ksh, it's pretty simple, used for uncompress zips and rars, menu based, simple menu though


#!/bin/ksh

# dir ---------> nombre de directorio
# numzip ------> cantidad de zips que hay en cada directorio
# zipname -----> nombre de cada zip en el directorio
# numrar ------> cantidad de rars que hay en cada directorio
# rarname -----> nombre de cada rar en el directorio
# log  --------> ruta de los logs
# data --------> directorio de software
# identstr ----> las 3 primeras letras de los ficheros, sirve como wild card

export log=e:\/download\/\/log
export data=e:\/download\/\/rips
export ddate=`date +%C%y%m%d`

unzip()
{
for dir in `grep ^ dir.txt`
do	
	cd $dir
	echo "checking for zips in $dir" >> $log\/${ddate}error.txt
	ls *.zip > zip.txt 2>> $log\/${ddate}error.txt
	if [ $? -eq 0 ]
	then
	numzip=`cat zip.txt | wc -l`
	identstr=`head -1 zip.txt | awk '{print substr($1, 1,3)}'`
	pkunzip -od $identstr\*.zip >> $log\/${ddate}uncompress.log
	status=$?
		if [ `ls *.zip | wc -l` -gt $numzip ]
		then
			echo "More zips were extracted in $dir" >> $log\/${ddate}error.txt
		fi
		case $status in
		0) for zipname in `grep ^ zip.txt`
		   do	
			rm $zipname
		   done
		   ;;
		1) echo "Pkunzip exits with error status 1"
		   ;;
		11) echo "CRC Error during zip extraction in $dir, Status 11" >> $log\/${ddate}error.txt
		    ;;
		*) print "Unexpected exit status, error in $dir" >> $log\/${ddate}error.txt
		   ;;
		esac
	fi
	cd $data
done
}

unrar()
{
for dir in `grep ^ dir.txt`
do	
	cd $dir
	echo "checking for rars in $dir" >> $log\/${ddate}error.txt
	ls *.r* > rar.txt 2>> $log\/${ddate}error.txt
	if [ $? -eq 0 ]
	then
	numrar=`cat rar.txt | wc -l`
	identstr=`head -1 rar.txt | awk '{print substr($1, 1,3)}'`
	rar x -z -y $identstr\*.rar >> $log\/${ddate}uncompress.log
	status=$?
		if [ `ls *.r* | wc -l` -gt $numrar ]
		then
			echo "More rars were extracted in $dir" >> $log\/${ddate}error.txt
		fi
		case $status in
		0) for rarname in `grep ^ rar.txt`
		   do	
			rm $rarname
		   done
		   ;;
		3) echo "CRC Error during rar extraction in $dir" >> $log\/${ddate}error.txt
		   ;;
		*) print "Unexpected exit status, error in $dir" >> $log\/${ddate}error.txt
		   ;;
		esac
	fi
	cd $data
done
}

cleanup()
for dir in `grep ^ dir.txt`
do	
	cd $dir
	rm rar.txt zip.txt *.diz 2>> $log\/${ddate}error.txt
	cd $data
done

echo "Please select uncompressing options"
echo "1. unzip only"
echo "2. unrar only"
echo "3. unzip & unrar"
echo "4. unzip, unrar & cleanup"
echo "5. cleanup only\n"

read option

echo "Error Log starting.........................................\n" > $log\/${ddate}error.txt
echo "Process Log starting.........................................\n" > $log\/${ddate}uncompress.log

case $option in
1) unzip
   ;;
2) unrar
   ;;
3) unzip
   unrar
   ;;
4) unzip
   unrar
   cleanup
   ;;
5) cleanup
   ;;
*) print "Please insert options 1 ~ 5"
   ;;
esac

or something like this.. i needed help once getting my script to repeat or bomb my menus and a nice person sent me this

#!/usr/bin/ksh
# simple menu - doesn't really do anything
# You may need to change this:
alias echoe="/bin/echo -e"

# functions:
func_1 () {
echoe "Yay \n\n"
}
func_2 () {
echoe "Boo \n\n"
}
menu_list () {
clear
                echoe "\n\n\t\tThis is the title\n\n\n"
                echoe "Make your choice: \n"
                echoe "To say Yay, press 1 \n"
                echoe "To say Boo, press 2 \n"
                echoe "To quit, press "q" \n"
}

go_ahead () {
tput smso
echoe "Press any key to return to the menu  \c"
tput rmso
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
dd bs=1 count=1 >/dev/null 2>&1
stty "$oldstty"
echoe
}
func_select () {
tput smso
echoe "\nPlease make your selection ( )\b\b\c"
read selection
tput rmso
case $selection in
     1) clear ; func_1 ; go_ahead ;; 
     2) clear ; func_2 ; go_ahead ;;
     q|Q) tput rmso ; clear ; exit 0 ;;
esac
}
#
# Here is where is gets looped - basically forever, until
# the "q" option is selected, causing an explicit exit
#

while `true`
do
menu_list
func_select
done

added code tags for readability --oombera