≡ Menu

Shell Script To Find Largest Among 3 Integers (Numbers) Given As Arguments

#!/bin/bash
# Shell code to find largest integer among the 3 integers given as arguments.
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2004 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
a=$1
b=$2
c=$3
if [ $# -lt 3 ]
then
	echo "$0 n1 n2 n3"
	exit 1
fi
if [ $a -gt $b -a $a -gt $c ]
then
	echo "$a is largest integer"
elif [ $b -gt $a -a $b -gt $c ]
then
	echo "$b is largest integer"
elif [ $c -gt $a -a $c -gt $b ];
then
	echo "$c is largest integer"
else
	echo "Sorry cannot guess number"
fi

Comments on this entry are closed.

  • sysadmn

    Um,
    echo -e "$a\n$b\n$c" | sort -rn | head -1
    or


    big = $a;
    [ $b -gt $big ] && big=$b;
    [ $c -gt $big ] && big=$c;
    echo $big

  • jacob

    It is quite helpful to know how the argument list can be easily handled. Thank you very much !! I was totally confused to see a lot of complex scripts. But, this one really helped me.

  • loganathan

    i need ordinary program for linux progran for find biggest among 3 numbers using command line arguments… please help me…

  • poo

    write a shell script that uses any encryption method to encrypt a file

  • dick-head

    HAHA sysadmn got that one

  • Anonymous

    what is the pupose of $# in the program

  • deep

    what is meaning of elif