Code sample: ksh

David G. Korn produced the Korn shell for unix to serve both as a command interpreter and a programming language.  It's not superfast, but it is versatile.

This particular program stores itself in memory as two shell functions, so that the system does not have to read it in line by line as it executes.

#!/bin/ksh
#
#

TTYSTATE=`stty -g`

typeset -i CNT
typeset -i i
typeset -i k
typeset -i l
typeset -i n

EXEDIR=/plugh/progdir

RPTDIR=${EXEDIR}/rpt
VIEW=${EXEDIR}/exe/viewer
USAGE="Usage: ${0} , where  is the extension of the set \
of filenames we are to look at."

EXTENS=${1:?${USAGE}}

if [ ! -x ${VIEW} ]; then
	echo "Can't use ${VIEW}"
	exit -1
fi

loadmenu() {

	i=0
	k=0
	l=1

	ls ${RPTDIR}/*.${EXTENS} 1>/dev/null 2>&1 || {
		echo "No such files in ${RPTDIR}!"
		exit -1
	}
	for FILE in `cd ${RPTDIR} 1>/dev/null; \
		ls -tr *.${EXTENS} |tail -50; cd - 1>/dev/null 2>&1`
	do
		j=`head -1 ${RPTDIR}/${FILE} | cut -c11-25`
		i=$i+1
		if [ "$i" -lt 10 ]
		then
			z='\0040'${i}
		else
			z=${i}
		fi
		SELLIST[${i}]=${FILE};
		n=`echo "$z $j\c"|wc -c`
		if [ "$n" -lt 8 ]; then j="$j\t"; fi
		if [ $k -eq 0 ]; then
			MENU[${l}]="$z $j\t"
			k=$k+1
		else
			MENU[${l}]="${MENU[$l]}\t$z $j\t"
			k=$k+1
		fi
		if [ $k -eq 3 ]; then
			k=0
			l=$l+1
		fi
	done

	CNT=${#MENU[*]}+1
	MENU[0]="\n\t\t-- SELECT FROM LIST --\n\n";
	MENU[${CNT}]="\t\t\t'X' to exit ";
}

runmenu() {

	while [ 1 -eq 1 ]
	do
		clear
		i=0
		while [ "${i}" -lt "${CNT}" ]
		do
			echo ${MENU[${i}]}
			i=${i}+1
		done
		echo
		echo ${MENU[${i}]}
		echo
		echo "\t\tEnter your selection: \c"
	
		read; case ${REPLY} in
			[Xx])
				stty ${TTYSTATE}
				exit;;
			[0-9]*)
				i=${REPLY}
				SEL=${SELLIST[${i}]}
				if [ -f "${RPTDIR}/${SEL}" ]
				then
					${VIEW} ${RPTDIR}/${SEL}
				fi;;
			*)
				echo "\0007\0007Didn't understand that!"
				sleep 3
				clear;;
		esac
	done
}

loadmenu

runmenu

Return to language index.


A publication of the School of Tyrannus

Copyright © 1998, The School of Tyrannus
All rights reserved