bin bash decimal compare


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bin bash decimal compare
# 1  
Old 07-27-2012
bin bash decimal compare

I need decimal comparing with if. Check if apache version is less than 2.2.17.

I tried this and not working.

Code:
#!/bin/bash

apachever=`/usr/local/apache/bin/httpd  -v | head -1 | awk '{print $3}' |cut -d/ -f 2`

if [[ $(echo "$apachever < 2.2.17" |bc) -eq 1 ]]; then
        echo "Apache version less than 2.2.17"
else
        echo "Apache version greater or equal to 2.2.17"
fi

# 2  
Old 07-27-2012
try with ksh
# 3  
Old 07-27-2012
@anil510: From math perspective, is "2.2.17" a number? How can you expect bc to evaluate two such strings?

One alternative is to first split the major version, minor version and build number, hold them in an array and then compare them.
# 4  
Old 07-27-2012
awk will compare the values

Code:
$ echo "2.2.25" | nawk '{if($0>"2.2.28"){print "Hi"}else{print "Hello"}}'
Hello
$ echo "2.2.25" | nawk '{if($0>"2.2.20"){print "Hi"}else{print "Hello"}}'
Hi

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 07-27-2012
Quote:
Originally Posted by itkamaraj
awk will compare the values

Code:
$ echo "2.2.25" | nawk '{if($0>"2.2.28"){print "Hi"}else{print "Hello"}}'
Hello
$ echo "2.2.25" | nawk '{if($0>"2.2.20"){print "Hi"}else{print "Hello"}}'
Hi

That won't work. That string comparison will consider 2.2.3 greater than 2.2.20. Each component of the version string needs to be considered separately and must be treated numerically.

Regards,
Alister
# 6  
Old 07-27-2012
Hi.

Note that some non-numeric characters might be found in some v.r.l strings:
Code:
       The version is always described with a triple <version,revision,level>
       and is represented by a string which always matches the regular
       expression ""[0-9]+\.[0-9]+[sabp.][0-9]+"".

-- excerpt from man shtool-version

although I have not seen the sabp in, for example, the apache2 version on my system. An example from the man page is 1.2b3

I have seen instances where an underline is used: java version "1.6.0_22"

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 7  
Old 07-27-2012
Code:
apachever=`/usr/local/apache/bin/httpd  -v | head -1 | awk '{print $3}' |cut -d/ -f 2`

Code:
apachever=$(httpd -v | awk -F'[ /]' '{print $4}')

IFS=. read -r v r l <<< "$apachever"
if (( v >= 2 && r >= 2 && l >= 17 )); then
  :
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Usage of #!/bin/sh vs #!/bin/bash shell scripts?

Some question about the usage of shell scripts: 1.) Are the commands of the base shell scripts a subset of bash commands? 2.) Assume I got a long, long script WITHOUT the first line. How can I find out if the script was originally designed für "sh" or "bash"? 3.) How can I check a given... (3 Replies)
Discussion started by: pstein
3 Replies

3. Shell Programming and Scripting

How to compare decimal values in bash?

Hi, I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out. I have tried using below scripts. But its giving me error. 1)amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }') if ;then echo "correct" else echo "Wrong"... (3 Replies)
Discussion started by: Siba Tripathy
3 Replies

4. Shell Programming and Scripting

How compare decimal values?

I have 2 files say tp1.txt and tp2.txt having following data cat tp1.txt abc,2.20,IN20 acb,3.15,DN10 bca,3,RD10 cat tp2.txt alv,1.00,IN20 aaa,4.05,DD10 abb,5.50,RD12 i want to compare the values on 2nd field of both the file, if value of first tp1.txt is greater than value... (3 Replies)
Discussion started by: ranabhavish
3 Replies

5. Shell Programming and Scripting

BIN/BASH.

THANKS UNIX SYSTEM®.I was found my job from UNIX®.I USE MONKEY WRENCH WITH WARTER.I am now studying my studio with UNIX SYSTEM®. THANKS UNIX SYSTEM®. THANKS OUR OPEN GROUP. from Takayasu Sakashita.My name is Takayasu Sakashita. I respect you. Austin.PEACE!Bey bey. Your friend TAKA.Good... (1 Reply)
Discussion started by: administrator®
1 Replies

6. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

7. Shell Programming and Scripting

Compare float value with single decimal

#!/bin/bash filter_file=/export/home/alvin/test.txt range1_count=0 range2_count=0 categorized(){ value=$1 if # range 5.1 to 10.0 then range1_count=`expr $range1+ 1` elif # range 5.1 to 15.00 then range2_count=`expr $range2+ 1` fi } #read txt file which contain lines of... (8 Replies)
Discussion started by: alvin0618
8 Replies

8. Shell Programming and Scripting

Bash Decimal Addition using bc

Hi guys/gals i have a quick question. I am trying to read from a file and add the values up but the problem is the values are not integers their floats so i tried to used bc but failed epicly lol. Any tips would be great. Thanks this is the code i have so far : while read num do ... (6 Replies)
Discussion started by: vb615
6 Replies

9. Shell Programming and Scripting

Compare integer value with decimal

Hi all, I have a issue... Is it possible to compare integer value with decimal value. If it is not possible,then how can i compare 2 decimal values in born shell.thanks! (3 Replies)
Discussion started by: MARY76
3 Replies

10. Shell Programming and Scripting

compare decimal numbers

Hi anyone, i need to compare two decimal numbers i thought that it could be do it with if but... :( So, i'm writing in csh and i really apreciate if anyone can help me if ( $ppl_kn <= $ppl_wb ) then echo "############# KNdiscount model has the lowest perplexity" set... (5 Replies)
Discussion started by: tmxps
5 Replies
Login or Register to Ask a Question