Thursday, May 13, 2010

Ubuntu: script to find largest number in 3 integers

Here small script for finding largest number in three integers

Open the text file via terminal

sudo gedit laint.sh

Copy and paste the following lines

#!/bin/bash
# Shell code to find largest integer among the 3 integers given as arguments.
# -------------------------------------------------------------------------

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

Save and exit from the file. Next

sudo chmod +x laint.sh

./laint.sh 90 70 40

the output is

90 is largest number

0 comments: