Find max number in a set


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find max number in a set
# 1  
Old 02-20-2010
Find max number in a set

Is there a simple way of calculating the max number in a set of variables, so
Code:
a=1
b=3
c=6
d=30

something that says
Code:
e=max($a, $b, $c, $d)

I've found a way to do it using:
Code:
a="1"
b="3"
c="5"
d="50"

if [ "$a" -gt "$b" ];
then
        t=$a
        else
        t=$b
fi
if [ "$t" -lt "$c" ];
then
        t=$c
fi
if [ "$t" -lt "$d" ];
then
        t=$d
fi
echo $t

is there a simpler, more elegant way to do it?

Last edited by unclecameron; 02-20-2010 at 01:27 PM..
# 2  
Old 02-20-2010
Like this?
Code:
a=1
b=3
c=6
d=30
m=0
for i in $a $b $c $d; do
  if [ $m -lt $i ]
  then
    m=$i
  fi
done
echo $m

Or:

Code:
max(){
  m=0
  for i in "$@"; do
    if [ $m -lt $i ]; then
      m=$i
    fi
  done
  echo $m
}

a=1
b=3
c=6
d=30
m=$(max $a $b $c $d)
echo $m


Last edited by Scrutinizer; 02-20-2010 at 01:56 PM..
# 3  
Old 02-20-2010
You could just save your second example as max, somewhere in your PATH, and call it as

Code:
e=`max $a $b $c $d`

Change the last line of the 'max' script to

Code:
echo -e "$t\c"

# 4  
Old 02-21-2010
This version is not cpu friendly in this case (previous are better), but example how to make generally when we have
need to do some for bigger list of values.
Code:
# create some output, sort it, take the 1st line
for f in $a $b $c $d
do
     echo $f
done | sort -n -r | head -1

Or more like a command
Code:
#!/usr/bin/ksh
#max.sh
for f in $*
do
     echo $f
done | sort -n -r | head -1

And using: give the values on the command line
Code:
chmod a+rx max.sh
./max.sh 233 444 22 444 55  666

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies

2. Programming

How to read max stack size -Xss that is set/default for a java program?

I need to know what is the maximum stack size i.e. -Xss my java program is running with. Is there a way to find that out from inside my java program code and outside of it. What i am looking for is to read whatever the current set max limit -Xss (stack sie) is for a particular JVM(not... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. Solaris

Max. number of NFS mounts

Hi, I was wondering, whether there is a limit regarding the max number of nfs mounts in Oracle Solaris 10 (newest update). The data center plans to migrate from a fibre channel based storage environment (hitachi) to a nfs based storage environment (netapp). Regarding the Solaris 10 database... (1 Reply)
Discussion started by: schms
1 Replies

4. Shell Programming and Scripting

Max number of variables?

Hi Folks.Just out of interest does anyone know if their is a maximum number of variables that korn shell supports and if so how do I query what it is?Cheers (1 Reply)
Discussion started by: steadyonabix
1 Replies

5. Linux

Max Number of remote X sessions

Hello all, I'm looking to implement a Linux server that will host up to 60 simultaneous X sessions, all running firefox to a secured web interface. Does anyone have any experience sizing a system like this? The reason for the setup isn't as important (Since I really don't understand why... (1 Reply)
Discussion started by: ZekesGarage
1 Replies

6. Shell Programming and Scripting

find max number

Hi, i have a file with numbers in it and i was wondering if there's a script i could use to find the max number and have that printed to a new file? example a.txt 18 26 47 34 27 so find the max number in a.txt and print it to b.txt. Thanks! (17 Replies)
Discussion started by: moonbaby
17 Replies

7. Solaris

Max. Possible number of IOs

Hi, Is there a way to find out the maximum possible number of IOs on a Solaris Servers. I'm using SUN Fire V240 (2 Replies)
Discussion started by: justsam
2 Replies

8. Solaris

Max number of Folder in Solaris 8

Hi, Anyone can help ? I have 21,000 sub-folder under the main folder . Now are having some problem writing . Is there a max no. of sub-folder can be create under Solaris . If yes ,what is the max no. , can it be configure to increase ? Thanks (4 Replies)
Discussion started by: civic2005
4 Replies

9. Shell Programming and Scripting

Max number of parameters to korn shell?

Hi All, what is the maximum limit for the command line arguments in korn shell. Regards, Raju (4 Replies)
Discussion started by: rajus19
4 Replies

10. UNIX for Dummies Questions & Answers

Max number of concurrent processes

OS - Sun OS7 What sources can I go to to figure out what is the maximun number of processes for OS7 with 2GB of memory. I believe it is 64K processes, but this number reflects resources being swaped. Any help is appreciated SmartJuniorUnix (1 Reply)
Discussion started by: SmartJuniorUnix
1 Replies
Login or Register to Ask a Question