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

How to find the biggest among 3 given numbers in Unix


##################################################################
# To find the biggest among 3 given numbers                                          #
##################################################################

#!/bin/ksh/

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

# Declaring and assigning values to the local variables
A=$1
B=$2
C=$3

#Logical part to find the largest number among 3 numbers
if [ $A -gt $B  -a  $A -gt $C ]
   then
   echo "A $A is the biggest number"
        elif  [ $B -gt $A  -a  $B -gt $C ]
        then
        echo "B $B is the biggest number"
                elif  [ $C -gt $A -a  $C -gt $B ]
                then
                echo "C $C is the biggest number"

   elif  [ $A -eq $B -a $A -eq $C -a $B -eq $C -a  $C -eq $B ]
   then
  echo "All numbers are same"
fi

exit 0