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
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • deep Mar 27, 2012 @ 4:53

    what is meaning of elif

    • sahir Nov 10, 2016 @ 9:01

      elif is nothing but it is like as else if in c

  • Anonymous Feb 16, 2012 @ 6:59

    what is the pupose of $# in the program

  • dick-head Feb 9, 2012 @ 20:48

    HAHA sysadmn got that one

  • poo Oct 28, 2010 @ 13:42

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

  • loganathan May 26, 2010 @ 6:27

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

  • jacob Jan 28, 2009 @ 11:17

    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.

  • sysadmn Jan 9, 2009 @ 20:48

    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

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.