#!/bin/sh -u # Sorts three integers on the command line; prompts for missing numbers. #------------------------------------------------------------ # Syntax: # $0 [ int1 [ int2 [ int3 ] ] ] #------------------------------------------------------------ # Purpose: # Get three integers from the command line or by prompting and reading. # Output the numbers in ascending order. # Save the numbers in a file. # Exit zero if the numbers were already sorted; exit 1 otherwise. # Print an error message and exit non-zero on any errors. #------------------------------------------------------------- # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: DAT2330 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Exercise 5, due March 21 # Comment: This is a sample assignment label. # see http://idallen.com/teaching/assignment_standards.html #------------------------------------------------------------- # Set standard search path and umask. # PATH=/bin:/usr/bin ; export PATH umask 022 # The name of the output file we will be writing. # outfile='out.txt' # Pick up any missing arguments by prompting on stderr and reading # them from standard input. Accept only zero, one, two, or three arguments. # If more than three, echo the count and incorrect input on stderr and exit. # if [ $# -eq 0 ]; then echo 1>&2 "Enter three integers:" read int1 int2 int3 || exit $? else if [ $# -eq 1 ]; then int1="$1" # first arg on command line becomes int1 echo 1>&2 "Enter two integers:" read int2 int3 || exit $? else if [ $# -eq 2 ]; then int1="$1" # first arg on command line becomes int1 int2="$2" # second arg on command line becomes int2 echo 1>&2 "Enter third integer:" read int3 || exit $? else if [ $# -eq 3 ]; then int1="$1" # first arg on command line becomes int1 int2="$2" # second arg on command line becomes int2 int3="$3" # third arg on command line becomes int3 else echo 1>&2 "$0: Expecting three integers; found $# args ($*)" exit 2 fi fi fi fi # We have collected the three arguments into $int1 $int2 and $int3. # Echo the user's input back to the user, as specified. # echo "You entered these inputs: '$int1' '$int2' '$int3'" # Print a nice message if any input number was empty. # if [ -z "$int1" -o -z "$int2" -o -z "$int3" ] ; then echo 1>&2 "$0: Empty integer input; nothing done. Bye." exit 2 fi # At this point, we should verify that each of the three inputs is # actually a valid integer (not letters); but, doing so is a bit messy. # See CST8129 for a way to write a shell function to do this nicely. # In DAT2330, we will just have to hope the data is good! -IAN! # Transfer the inputs to other variables and then bubble sort them. # sorted1="$int1" sorted2="$int2" sorted3="$int3" # Two compares and swaps make sorted3 the largest integer. # if [ "$sorted1" -gt "$sorted2" ] ; then tmp="$sorted1" sorted1="$sorted2" sorted2="$tmp" fi if [ "$sorted2" -gt "$sorted3" ] ; then tmp="$sorted2" sorted2="$sorted3" sorted3="$tmp" fi # sorted3 now has the biggest integer; sort the remaining two. # if [ "$sorted1" -gt "$sorted2" ] ; then tmp="$sorted1" sorted1="$sorted2" sorted2="$tmp" fi echo "The numbers are, from smallest to largest: $sorted1 $sorted2 $sorted3" # Put the sorted numbers into the $outfile output file, if the file # doesn't exist. (Exit the script if the file exists.) # if [ -e "$outfile" ] ; then echo 1>&2 "$0: File '$outfile' already exists. No output saved." exit 2 fi # BONUS: A writable, empty file is okay. # #if [ -s "$outfile" ] ; then # echo 1>&2 "$0: Non-empty file '$outfile' already exists. No output saved." # exit 2 #fi #if [ -e "$outfile" -a ! -w "$outfile" ] ; then # echo 1>&2 "$0: Existing file '$outfile' is not writable. No output saved." # exit 2 #fi echo "$sorted1" >"$outfile" echo "$sorted2" >>"$outfile" echo "$sorted3" >>"$outfile" count=$( wc -c <"$outfile" ) echo "The file '$outfile' contains $count characters." # Return 0 if the numbers were already sorted # if [ "$int1" -eq "$sorted1" \ -a "$int2" -eq "$sorted2" \ -a "$int3" -eq "$sorted3" ] ; then return=0 else return=1 fi exit $return