#!/bin/ksh
usage ()
{
if [[ $# -ne 2 ]]
then
print -u2 "Usage: $0 file1 file2"
exit 1
fi
}
# Default temporary directory
: ${TEMPDIR:=/tmp}
# Check argument count
usage "$@"
# Set up temporary files for sorting
file1=$TEMPDIR/file1.$$
file2=$TEMPDIR/file2.$$
# Sort
sort $1 > $file1
sort $2 > $file2
# Open files $file1 and $file2 for reading.  Use FD's 3 and 4.
exec 3<$file1
exec 4<$file2
# Read the first line of each file to figure out how to start.
read -u3 Line1
Status1=$?
read -u4 Line2
Status2=$?
# Strategy: while there's still input left in both files:
#       Output the lesser line.
#       Read a new line from the file that line came from.
while [[ $Status1 -eq 0 && $Status2 -eq 0 ]]
do
	if [[ ! "$Line1" > "$Line2" ]]
		then
		print "1.\t$Line1"
		read -u3 Line1
		Status1=$?
	else
		print "2.\t$Line2"
		read -u4 Line2
		Status2=$?
	fi
done
# Now one of the files is at end-of-file.
# Read from each file until the end.
# First file1:
while [[ $Status1 -eq 0 ]]
do
	print "1.\t$Line1"
	read -u3 Line1
	Status1=$?
done
# Next file2:
while [[ $Status2 -eq 0 ]]
do
	print "2.\t$Line2"
	read -u4 Line2
	Status2=$?
done
# Close and remove both input files
exec 3<&- 4<&-
rm -f $file1 $file2
exit 0
