How to compare version values in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare version values in shell script?
# 1  
Old 05-14-2012
How to compare version values in shell script?

Hi,
I need to compare two versions values and report only true or false depending on their difference result.

like -
Code:
echo $(echo "1.8 >= 2.0" | bc)
0

echo $(echo "2.0 >= 2.0" | bc)
1

but my task is to compare values like -

Code:
echo $(echo "1.9.1 >= 2.0" | bc)
(standard_in) 1: syntax error

Any idea how can i do it in shell script?

Last edited by methyl; 05-14-2012 at 08:25 AM.. Reason: please use code tags
# 2  
Old 05-14-2012
What Operating System and version are you running and what Shell are you using?

Code:
# String comparison. This is not a numeric comparison
if [ "2.0" = "2.0" ]
then
      echo "1"
else
      echo "0"
fi

The above does not work for "greater than" comparisons of version numbers. To do that, you would need to break the version string into numeric components and compare each component one-by-one. You'd probably have to normalise each version number for the comparision (i.e. 1.0 becomes say 1.0.0.0.0). This script is going to get quite complicated.

Last edited by methyl; 05-14-2012 at 08:41 AM..
# 3  
Old 05-14-2012
I am using red hat 6.1 and bash shell.

I want comparison for greater than or equal to and one of the variable has 3 parts like 1.9.2 which shall be compared with 2.0 for example.
# 4  
Old 05-14-2012
Hi.

Here is a bash function that is adapted from a ksh function:
Code:
#!/usr/bin/env bash

# @(#) user4	Compare version numbers of form a.b.c.
# Adapted from post # 10.
# https://www.unix.com/unix-dummies-questions-answers/
# 93739-comparing-version-numbers.html#post302269675

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

# compare version numbers
# usage: vercmp <versionnr1> <versionnr2>
#         with format for versions xxx.xxx.xxx
# returns: 0 if versionnr1 equal or greater
#          1 if versionnr1 lower

vercmp()
{
  local a1 b1 c1 a2 b2 c2
  # echo|read succeeds in ksh, but fails in bash.
  # bash alternative is "set --"
  db "input 1 \"$1\", 2 \"$2\" " 
  v1=$1
  v2=$2
  db "v1 $v1, v2 $v2"
  set -- $( echo "$v1" | sed 's/\./ /g' )
  a1=$1 b1=$2 c1=$3
  set -- $( echo "$v2" | sed 's/\./ /g' )
  a2=$1 b2=$2 c2=$3
  db "a1,b1,c1 $a1,$b1,$c1 ; a2,b2,c2 $a2,$b2,$c2"
  ret=$(( (a1-a2)*1000000+(b1-b2)*1000+c1-c2 ))
  db "ret is $ret"
  if [ $ret -lt 0 ] ; then
    v=-1
  elif [ $ret -eq 0 ] ; then
    v=0
  else
    v=1
  fi
  printf "%d" $v
  return
}

FILE=${1-data1}

while read a b
do
  pe
  pe " Comparing version strings \"$a\" and \"$b\""
  v=$( vercmp $a $b )
  db "vercmp returns $v"
  if [ $v -lt 0 ]
  then
    pe " $a is earlier than $b"
  elif [ $v -eq 0 ]
  then
    pe " Versions are equal"
  else
    pe " $a is later than $b"
  fi
done <$FILE

exit 0

producing:
Code:
% ./user4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39

 Comparing version strings "1.1.0" and "1.1.1"
 1.1.0 is earlier than 1.1.1

 Comparing version strings "2.2.2" and "2.2.1"
 2.2.2 is later than 2.2.1

 Comparing version strings "3.3.9" and "3.4.1"
 3.3.9 is earlier than 3.4.1

 Comparing version strings "3.5.0" and "3.5.0"
 Versions are equal

 Comparing version strings "1.9.2" and "2.0"
 1.9.2 is earlier than 2.0

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell script to compare values between servers

Hi I am a beginner in UNIX and shell scripting and i have a requirement as stated below, requirement Login to a unix server 1 1. connect to a database mysql -uUsername -pPassword 2. Select a Schema : "Use Schemaname1" 3. query for a particular record ... (2 Replies)
Discussion started by: Hamdul
2 Replies

2. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

3. UNIX for Dummies Questions & Answers

How to compare to values returned from sql in shell scripting?

hey i am using this code to connect to sql , store the value in variable and then compare it with another variable after some time by executing the same query but the desired result is not coming #!/bin/bash val=$(sqlplus -s rte/rted2@rel76d2 <<ENDOFSQL set heading off set feedback off... (11 Replies)
Discussion started by: ramsavi
11 Replies

4. Shell Programming and Scripting

LINUX - How to compare the values in 2 files & exit from the script

Hi All, I have a requirement where I need to compare 2 files & if the values in the files match, it should proceed, else exit the script without proceeding further. For e.g : Scenario 1 In this case, the script should exit without proceeding further. Scenario 2 In this case, the script... (7 Replies)
Discussion started by: dsfreddie
7 Replies

5. Shell Programming and Scripting

Compare values for a pattern match and execute script

Here in the input file 23:59:13,devicename,21,server1,700 23:59:13,devicename,22,server2,200 23:59:13,devicename,23,server3,200 23:59:13,devicename,24,server4,200 23:59:13,devicename,25,server5,200 23:59:13,devicename,26,server6,200 23:59:13,devicename,27,server7,200... (6 Replies)
Discussion started by: necro98
6 Replies

6. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

7. Shell Programming and Scripting

Shell script to run command + compare values and post a warning message.

Hi all, I am trying to create shell script to run command then compare values with rule set for every 5 seconds and post a warning message if the compared value meet the rules. -the script is related to Oracle/Sun product messaging server 6.2 1) command that need to be run to gather... (2 Replies)
Discussion started by: Mr_47
2 Replies

8. 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

9. Shell Programming and Scripting

Version Control Through the Shell Script

Version Control Through the Shell Script Hi Guys, Apologize for the big request, please take some time and read it completely... This is Very important for me, and ur help is Very much Appriciated. I want to maintain the Version control to all my scripts running in Production server, I am... (6 Replies)
Discussion started by: Anji
6 Replies

10. Shell Programming and Scripting

open 2 files and compare values script - urgent

Hi gurus I have two csv files that are outputs. The file contains data similar to s.no,number1,number2,date1 -------------------------------- 1, a123,482.29,11/28/07 13:00 2,a124,602.7,9/24/07 14:00 3,a125,266.93,10/9/07 16.48 4,a126,785.15,11/14/07 16:08 <file 2> s.no name... (2 Replies)
Discussion started by: inkyponky
2 Replies
Login or Register to Ask a Question