3 numbers and return the maximum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 3 numbers and return the maximum
# 1  
Old 11-20-2010
3 numbers and return the maximum

Hello,
I am doing bash and for that doing excersices. The folowing one asks for 3 numbers and is suppose to return the maximum. An error should be returned if on of the numbers is missing. I managed to solve it. Can you give me other ways or advices.

Code:
#!/bin/bash
#Date: 2010.10.19
#Ecrire un script qui prend en paramèe trois entiers et qui affiche le plus grand. On affichera un message d'erreur s'il n'y pas pas trois arguments passésur la ligne de commande

echo "Entree trois entiers; separee par un espace";read numb1 numb2 numb3

MAX="0"

for i in $numb1 $numb2 $numb3 ; do
    echo "$i"
    if [ $MAX -lt $i ] ; then
        MAX=$(echo $i)
    fi
done

echo "Le plus grand nombre est: $MAX"

#END

# 2  
Old 11-20-2010
You could do something like:
Code:
for i in $numb1 $numb2 $numb3 ; do
    echo "$i"
    if [ `echo "$i" | awk '{ print (int($0)==$0)}'` == 0 ]; then 
       echo "$i" is not numeric
       exit 1
    fi
    if [ $MAX -lt $i ] ; then
        MAX=$(echo $i)
    fi
done


Last edited by Franklin52; 11-20-2010 at 01:08 PM.. Reason: Edit: replaced $1 with $i
# 3  
Old 11-21-2010
Code:
echo "Entree trois entiers; separee par un espace";read numb1 numb2 numb3

MAX=0
counter=0

for i in $numb1 $numb2 $numb3 ; do
    echo "$i"
    counter=$(( ${counter} + 1 ))
    if [ $MAX -lt $i ] ; then
        MAX=$(echo $i)
    fi
done

if [ ! $counter -eq 3 ]
then
    echo "Please enter 3 numbers (not ${counter} numbers)"
else
    echo "Le plus grand nombre est: $MAX"
fi

# 4  
Old 11-21-2010
Added test for 3 values (uses array numbs) and test value entered is numeric. Errors printed to stderr and exit status set.

Code:
#!/bin/bash
#Date: 2010.10.19
#Ecrire un script qui prend en paramèe trois entiers et qui affiche le plus grand. On affichera un message d'erreur s'il n'y pas pas trois arguments passésur la ligne de commande
echo "Entree trois entiers; separee par un espace";read -a numbs
MAX="0"
if [ ${#numbs[@]} -ne 3 ]
then
    echo "Error: 3 numbers required" >&2
    exit 1
fi
for i in ${numbs[@]} ; do
    if [ $i -gt 0 ] 2> /dev/null || [ $i -le 0 ] 2> /dev/null
    then
        if [ $MAX -lt $i ] ; then
            MAX=$i
        fi
    else
        echo "Error: $i is not numeric" >&2
        exit 2
    fi
done
echo "Le plus grand nombre est: $MAX"


Last edited by Chubler_XL; 11-21-2010 at 09:15 PM.. Reason: Updated to use bash arrays
# 5  
Old 11-27-2010
Thanx to all I have reviewed all you solutions. I learned a great deal.

@Chubler_XL

What is this test for. and why do you send to /dev/null?
Quote:
if [ $i -gt 0 ] 2> /dev/null || [ $i -le 0 ] 2> /dev/null
BR.

---------- Post updated at 03:58 AM ---------- Previous update was at 03:56 AM ----------

Quote:
Originally Posted by Franklin52
You could do something like:
Code:
for i in $numb1 $numb2 $numb3 ; do
    echo "$i"
    if [ `echo "$i" | awk '{ print (int($0)==$0)}'` == 0 ]; then 
       echo "$i" is not numeric
       exit 1
    fi
    if [ $MAX -lt $i ] ; then
        MAX=$(echo $i)
    fi
done

thx for the Solution. But I don't know how tu use awk yet. But you did not include the test for 3 numbers.
BR.
# 6  
Old 11-27-2010
Alternatively tou could use a case statement, for example:
Code:
for i in $numb1 $numb2 $numb3 ; do
    case $i in *[^0-9]*)
      echo "$i" is not numeric
      exit 1
    esac
    if [ $i -gt $MAX ] ; then
      MAX=$i
    fi
done
echo $MAX

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

3. Shell Programming and Scripting

Regex - Return numbers of exactly 8 digits

Hi All I am new to this forum and also regex. I am using bash scripting and have a file like this "0012","efgh","12345678","adfdf", "36598745" "87654321","hijk","lmno" I want the ouput to be 12345678 36598745 87654321 Criteria like this - number - 8 carachters long Please let... (21 Replies)
Discussion started by: buttseire
21 Replies

4. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

5. Shell Programming and Scripting

Return part of string after X numbers of a character

My input strings look something like this: /dev/vs/dsk/group/vol I want to print just "group" out of the line... which is always found between the 4th and 5th "/" in the string. I figure I have to use awk, sed, or some combination to do this, but I've searched the forums and can't find... (2 Replies)
Discussion started by: ltricarico
2 Replies

6. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

7. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies

10. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies
Login or Register to Ask a Question