Saturday, October 6, 2012

How to find the Largest and Smallest number in the given numbers in Unix


###############################################################
# To find the Largest and Smallest number in the given numbers      #
###############################################################

#!/bin/ksh

# To make sure sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please provide the sufficient arguments"
  echo "bigsmall file_name"
  exit -1
fi

# Declaring and Assigning values to the local variables
set -A LIST `cat /clocal/fme/prod/forecast/padu/Lilly/numberlist.txt`
a=${#LIST[*]}
Small=${LIST[0]}
Big=${LIST[0]}

# Implementation of logic to find the biggest and smallest number
for i in ${LIST[*]}
do
 if [ $i -lt $Small ]
  then
  Small=$i
 elif [ $i -gt $Big ]
  then
  Big=$i
 fi
done
 echo "Smallest = $Small"
 echo "Biggest = $Big"

exit 0