Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using the "Less Than" (-lt) switch on numbers with decimal places?
# 1  
Old 05-19-2009
Question 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.
# 2  
Old 05-19-2009
remove the dots as well so that you can compare it as a number.. 111 will still be "lower" version than 112.
# 3  
Old 05-19-2009
# 4  
Old 05-19-2009
Code:
DEPLOYED_VERSION=`ccm ls ${TARGET_FILE} | cut -f2 -d"-" | sed 's/://g'`



-Devaraj Takhellambam
# 5  
Old 05-19-2009
Thanks everybody for your help.

Quote:
Originally Posted by devtakh
Code:
DEPLOYED_VERSION=`ccm ls ${TARGET_FILE} | cut -f2 -d"-" | sed 's/://g'`

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

Code:
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
# 6  
Old 05-19-2009
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

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

-Devaraj Takhellambam
# 7  
Old 05-20-2009
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..Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #414
Difficulty: Easy
JavaScript is a high-level, interpreted scripting language that conforms to the ECMAScript specification.
True or False?

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

decimal output "bc not working"

i have variable a=5 and b=1 i want to do b/a i.e 1/5 and want decimal output if i tried with bc cmd then it will give ans without decimal digits. please help me. (4 Replies)
Discussion started by: RahulJoshi
4 Replies

7. Shell Programming and Scripting

find and "-print" switch

I would like to know what is "-print" switch for. I read the man page but still I don't understand why we need that switch. Thank you very much! (13 Replies)
Discussion started by: MartyIX
13 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Convert "text" to "packed-decimal"?

Is there a way with HP-UX Release 10.20 (but going to HP-UX 11) to convert a regular "text" file to a packed data format (such as is created by a Cobol program)? (2 Replies)
Discussion started by: HuskyJim
2 Replies

Featured Tech Videos