Picking high and low variables in a bash script - possible?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Picking high and low variables in a bash script - possible?
# 1  
Old 11-25-2009
Picking high and low 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!
# 2  
Old 11-25-2009
You can do somthing like that :
Code:
c0=100
c1=12
c2=777
c3=1

clow=$c0
chigh=$c0

(( $c1 < $clow )) && clow=$c1
(( $c2 < $clow )) && clow=$c2
(( $c3 < $clow )) && clow=$c3

(( $c1 > $chigh )) && chigh=$c1
(( $c2 > $chigh )) && chigh=$c2
(( $c3 > $chigh )) && chigh=$c3

echo "Low : $clow"
echo "High: $chigh"

Output
Code:
Low : 1
High: 777

Jean-Pierre.
# 3  
Old 11-25-2009
Another way ...

Code:
$clow=$(echo -e "$c0\n$c1\n$c2\n$c3" | sort -n | head -1)
$chigh=$(echo -e "$c0\n$c1\n$c2\n$c3" | sort -n | tail -1 )

# 4  
Old 11-25-2009
check the man pages for sort. then once the data is sorted use sed to print out the first and last item.

here's an example using ls, i'll sort files by size then print the largest


Code:
mo@mo-laptop:~/scripts$ ls -l | awk '{ print $5 }' | sort -nr | sed -e '2,$d'
1366
mo@mo-laptop:~/scripts$

i used awk to strip everything out of the output except the file size. if your positional parameters are only number, this is unecessary
# 5  
Old 11-25-2009
Or a variation on Aigles' script using a loop:
Code:
c0=100
c1=12
c2=777
c3=1
clow=$c0
for i in {0..3}; do
  eval "((c$i < clow)) && clow=\$c$i"
  eval "((c$i > chigh)) && chigh=\$c$i"
done
echo "Low : $clow"
echo "High: $chigh"

# 6  
Old 11-25-2009
Thanks for the replies, all. I like chakrapani's code the best because it's only two lines Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

Hi All, I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help? Sample file: TS3*1451575*12*20151231*4*482.44 NM1*QC*1*CUTLER*BETTY DTM*472*20150808... (4 Replies)
Discussion started by: Venkata Prasad
4 Replies

2. Shell Programming and Scripting

Combine splitted low & high byte files into one file

Hi all, i have a binary file splitted into 2 chunks, first part with all high bytes and the second part with all low bytes. I need to combine the two chunks into one binary file like (eg. exactly the reverse of the splitting method solved in the thread # 130940) Hi bytes file content:... (7 Replies)
Discussion started by: mzs
7 Replies

3. Red Hat

High RAM usage, extremely low swapping

Hi team I have three physical servers running on Red Hat Enterprise Linux Server release 6.2 with the following memory conditions: # cat /proc/meminfo | grep -i mem MemTotal: 8062888 kB MemFree: 184540 kB Shmem: 516 kB and the following swap conditions: ... (6 Replies)
Discussion started by: hedkandi
6 Replies

4. AIX

High Runqueue (R) LOW CPU LOW I/O Low Network Low memory usage

Hello All I have a system running AIX 61 shared uncapped partition (with 11 physical processors, 24 Virtual 72GB of Memory) . The output from NMON, vmstat show a high run queue (60+) for continous periods of time intervals, but NO paging, relatively low I/o (6000) , CPU % is 40, Low network.... (9 Replies)
Discussion started by: IL-Malti
9 Replies

5. UNIX for Dummies Questions & Answers

Kernel/ user space and high/ low mem

Need some clarification on this.... 1. how are kernel/ user spaces and high/low memory related? 2. What do they all mean when i have the kernel command line as: "console=ttyS0,115200 root=/dev/sda2 rw mem=exactmap memmap=1M@0 memmap=96M@1M irqpoll" or 2. what do mem and memmap mean in... (3 Replies)
Discussion started by: dragonpoint
3 Replies

6. Shell Programming and Scripting

Split file into chunks of low & high byte

Hi guys, i have a question about spliting a binary file into 2 chunks. First chunk with all high bytes and the second one with all low bytes. What unix tools can i use? And how can this be performed? I looked in manpages of split and dd but this does not help. Thanks (2 Replies)
Discussion started by: basta
2 Replies

7. Shell Programming and Scripting

low & high values

on the file Ftp'd from the mainframe ,do we have any UNIX command to replace mainframe low and values to space or null. i tried using tr and it doesn't work ... Thanks (1 Reply)
Discussion started by: rlmadhav
1 Replies

8. UNIX for Dummies Questions & Answers

malloc returning NULL if freemem high & swapmem low (MPRAS version 3.03 )

Hi All,:) In my application malloc is returning NULL even though there is sufficient amount of free memory available but the swap memory is low. Is this possible that, if free memory is high & swap memory is low, malloc will not be able to allocate memory & return NULL ? Few details: ... (4 Replies)
Discussion started by: Ritesh Kumar
4 Replies

9. Solaris

malloc returning NULL if freemem high & swapmem low

Hi All, In my application malloc is returning NULL even though there is sufficient amount of free memory is available but swap memory is low. Is this possible that, if free memory is high & swap memory is low, malloc will not be able to allocate memory & return NULL ?:) Kindly look into... (5 Replies)
Discussion started by: Ritesh Kumar
5 Replies
Login or Register to Ask a Question