Using the "Less Than" (-lt) switch on numbers with decimal places?

I'm using shell scripts to run some commands for the Configuration Management tool called Telelogic Synergy.

I need to get the script to compare version numbers of files. The version numbers of the files are part of the actual filename, such as the following example:

example_file-1
example_file-2

I retrieve the version number of the files by doing a "cut" on the file name.

This is an extract from my script (the "ccm ls" part is CM Synergy command) where I'm using the cut command to read the file version:

DEPLOYED_VERSION=`ccm ls ${TARGET_FILE} | cut -f2 -d"-"`

The next line compares versions of a file:

if [ $TARGET_FILE_VERSION -lt $DEPLOYED_VERSION ]

This works fine when file version numbers are whole numbers, but if I have a version of a file with decimal places such as "example_file-1.1.1", then the script thinks that the "1.1.1" is a text string rather than a number and then throws up the "Integer expression expected" error message against the line of the script where I'm using the "-lt" switch.

Does anybody know how I can get the script to recognise numbers with decimal places as a numerical value instead of a text string?

Hope this makes sense, thanks in advance.

remove the dots as well so that you can compare it as a number.. 111 will still be "lower" version than 112.

maybe this helps: Compare integer value with decimal

DEPLOYED_VERSION=`ccm ls ${TARGET_FILE} | cut -f2 -d"-" | sed 's/://g'`

-Devaraj Takhellambam

Thanks everybody for your help.

Should the sed command not be ( using "." instead of ":" ):

DEPLOYED_VERSION=`ccm ls ${TARGET_FILE} | cut -f2 -d"-" | sed 's/.//g'`

?

I'm not sure why I'd need the 'g either?

Many thanks

Correct. you should use a dot(.) and escape it
and g is required bcoz u want to replace every occurances of it. If you dont specify it will replace only the first occurance, which u dont want

DEPLOYED_VERSION=`ccm ls ${TARGET_FILE} | cut -f2 -d"-" | sed 's/\.//g'`

-Devaraj Takhellambam

Thanks Devaraj

The problem with this though, is that if I have version 34 of a file deployed and somebody comes along and deploys version 29.1.1 of a file (which I don't won't to be deployed because it's an earlier version), the code will turn it into version 2911, which the script will think is a higher version number than 34...

Just to clarify, the script I'm working on is used for the purpose of making sure that earlier versions of scripts cannot be released into our production database, when there is a later version of the same script already deployed.

Version numbers with the dots in, are what we call parallel version numbers. If I have version number 29 of a script deployed, and a developer wants to work on it. He checks out version 29, thus making a new version - version 30. Then another developer may come along and need to do something else to the script. The second developer will check out version 29 of the script and his new version of the script will be version 29.1.1

If you understand that, you truly are a God..:smiley:

You should normalize versions before comparing them as strings.

One way to normalize:
34 => 34.00.00
29.1.1 => 29.01.01

If normalization is not an option, you have to implement the exact rule that your mind applies when comparing versions.