You are not logged in.

#1 2009-11-25 09:02:04

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

sorting variables in a bash script - possible?

Is it possible to have a bash script pick the highest and lowest values of four variables?  I've been googling for this but haven't come up with anything.  I have a script that assigns variables ($c0, $c1, $c2, and $c3) based on the coretemps from grep/sed statements of sensors.  I'd like to also assign two new variables ($chigh and $clow)  that will be the highest and lowest of the original four but I don't know how to go about it in bash.

Thanks!

Last edited by graysky (2009-11-25 09:03:36)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2009-11-25 09:03:55

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,495
Website

Re: sorting variables in a bash script - possible?

> a=10
> b=20
> (( a < b )) && echo $a || echo $b
10
> b=5
> (( a < b )) && echo $a || echo $b
5

Offline

#3 2009-11-25 09:41:20

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: sorting variables in a bash script - possible?

Building up from Allan's for four

highest=0
lowest=999
for value in 40 100 20 80; do (( value < lowest )) && lowest=$value; (( value > highest )) && highest=$value; done
echo HIGH: $highest
echo LOW: $lowest

Offline

#4 2009-11-25 14:51:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Re: sorting variables in a bash script - possible?

Thanks guys.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2009-11-25 15:08:15

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: sorting variables in a bash script - possible?

just for shits and giggles

LC_ALL=C range=( $( (for value in 40 100 20 80; do echo $value; done) | sort -n ) )

echo HIGH: ${range[${#range[@]}-1]}
echo LOW:  ${range[0]}

Offline

Board footer

Powered by FluxBB