Need semi-non-trivial example of an (MKS) ksh script [really programming-by-example]

rand at mks.UUCP rand at mks.UUCP
Thu Feb 26 01:26:59 AEST 1987


In article <948 at orcisi.UUCP>, michael at orcisi.UUCP writes:
> We're received a copy of MKS's Unix-like tool kit and it looks pretty
> good.
> 
> Currently I'm trying to convert a csh script that summarizes the output
> of du to a ksh script under MKS's Korn shell.
> 
> Can someone help me by forwarding me an example ksh script that
> contains a nested if-then-else if-endif sequence and/or a loop?
> 
> Thanx.

The following is the mainline shell script of the "Amazing Awk
Assembler" that was posted to mod.sources by Henry Spencer 8-10 months
ago.  I have included this script as an excellent demonstration of
programming the Korn Shell of the MKS Toolkit.  I tested this script
running on an IBM PC/AT running PC-DOS and the MKS Toolkit, and it ran
just fine.  Please note that this is not the complete assembler as the
awk scripts and test programs are not included.

---- File aaa.ksh,  Cut here ---

machdir=.		# Directory holding machine definitions.
machine=anon		# Default machine.
lib=auxil		# Directory holding machine-independent auxiliaries.

# set -- `getopt 'm:s:o:' $*`
if test $? != 0
then
	echo 'Usage: aaa [-m machine] [-s startaddr] [-o offset] [file] ...' >&2
	exit 2
fi
for f
do
	case "$f"
	in
		-s)		# Runtime start address.
		startaddr="$2"
		shift
		shift
		;;

		-o)		# Offset for code location counter.
		offset="$2"
		shift
		shift
		;;

		-m)		# Machine.
		machine="$2"
		shift
		shift
		;;

		--)
		shift ; break
		;;
	esac
done

for f
do
	echo "$f:"
	bn=`basename $f .s`
	# Do we have a machine specification in the file?
	machline=`sed 1q $f`
	case "$machline"
	in
		'#m'*)
		machine=`expr "x$machline" : 'x#m[ 	]\(.*\)'`
		;;
	esac
	# Is our machine specification valid?
	if test -d $machdir/$machine
	then
		machine=$machdir/$machine
	elif test ! -d $machine
	then
		echo "aaa: unknown machine $machine" >&2
		exit 1
	fi
	# Unsugar the syntax, change to one byte/line, do base conversions,
	# cope with machine-specific notation, and assign addresses to code
	# and values to symbols.
	sed -f $lib/unsugar $f | tr -s ' 	;' '\012' | awk -f $lib/base |
		awk -f $machine/notn | awk -f $lib/defs >tmp1
	# Get symbol definitions, merge with the predefined symbols.
	sed -n '/=/s//	/p' tmp1 >$bn.defs
	sort $machine/predef $bn.defs >tmp2
	# Get code lines, sort by contents.
	egrep '	' tmp1 | sort +1 >tmp3
	# Plug definitions into code, sort by location again, postprocess
	# for byte extraction and PC-relative offset arithmetic.
	join -a1 -j1 2 -o 1.1 2.2 1.2 '-t	' tmp3 tmp2 | sort -n |
		awk -f $machine/final >$bn.a
	# Make a feeble attempt to detect errors.
	awk '$2 !~ /^[0-9]+$/ || $2 < 0 || $2 > 255' $bn.a
	# Finally, turn into hex.  The "0\t0" is a kludge to cause a
	# buffer flush at the end.
	(cat $bn.a ; echo '0	0') |
		awk -f $lib/hex start=$startaddr offset=$offset - |
		tr 'a-f' 'A-F' >$bn.x
	# Clean up.
	rm tmp1 tmp2 tmp3
done
---- End of file aaa.ksh ----

	Randall Howard, MKS Inc.
	(519)-884-2251
	{decvax|allegra|ucbvax}!watmath!mks!rand



More information about the Comp.unix.questions mailing list