Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When using this ubuntu command:

${HOME}/temp/.git describe --always --tags HEAD 

the output of this command is:

v0.1.5-2-p343h3d3

I want to extract 1.5 from the output above and check if it's greater than 1.5 or not.
Is there any solution for this.?

share|improve this question
    
Why not 0.1.5? The 0 seems relevant... –  Oli Mar 27 at 11:37
    
@Oli I just want to check 0.1.5 is greater than 0.1.5 or not. inclusion of 0 is not matter for me. –  Prakash V Holkar Mar 27 at 11:46
1  
What about in the future when it might return something like "v1.1.0-3-p343h3d3" OR "v2.0.7-4-p343h3d3" OR "v10.0.9-5-p343h3d3"? If you ignore the "leading" number (the digit 0 from "v0" in your example), these will look like "1.0", "0.7", and "0.9", so these will all fail your test, even though you will probably want them all to pass your test of being ">=1.5". –  Kevin Fegan Mar 27 at 15:30

3 Answers 3

up vote 2 down vote accepted

You could do something like

${HOME}/temp/.git describe --always --tags HEAD  | awk ' {
                             match ($0, /[0-9]+\.[0-9]+\.[0-9]+/,m)
                             if(m[0]>"0.1.5"){ 
                                 print("hello")
                              }
                             }'

This will check the first matching regex result. Just throwing another way to do this, although I'm not sure how much of a practical way this is for you.

Cheers

share|improve this answer
    
Warning on string comparison: Version 0.1.33 is considered a "lower" version here –  Izkata Mar 27 at 16:29
    
It's an interesting angle but it's not numeric enough to work. echo | awk '{if("0.1.10">"0.1.5"){ print "oh hai" }}' –  Oli Mar 28 at 8:39
    
Of course, numeric comparison on string is not recommended. This is like a quick hack. –  rahules Mar 31 at 14:48

Version numbers are actually pretty tough to compare given that they're often not standard mathematical comparisons (this one for instance has two decimal points that makes bc throw up).

Therefore I turn to a lesser-known option in sort that can be used to sort version strings. Consider the following:

$ echo -e "v0.1.5\nv0.1.6-2-p343h3d3" | sort -V
v0.1.5
v0.1.6-2-p343h3d3

oli@bert:~$ echo -e "v0.1.5\nv0.1.4-2-p343h3d3" | sort -V
v0.1.4-2-p343h3d3
v0.1.5

This basically means we can sort the versions so the latest is on the last line. All we have to do then is a string comparison against the last line, inside an if, or a shortcutted-if:

[[ $(echo -e "v0.1.5\nv0.1.4-2-p343h3d3" | sort -V | tail -1) != "v0.1.5" ]] && echo NEWER

Play around with the v0.1.4-2-p343h3d3 string. If you stick it up to 0.1.6 it'll echo out.

Now to bring this all back around to your command:

[[ $(echo -e "v0.1.5\n$(${HOME}/temp/.git describe --always --tags HEAD)" | sort -V | tail -1) != "v0.1.5" ]] && echo NEWER

Or you could break it down for readability:

THRESHOLD="v0.1.5"
VERSION=$(${HOME}/temp/.git describe --always --tags HEAD)
if [[ $(echo -e "$THRESHOLD\n$VERSION" | sort -V | tail -1) != "$THRESHOLD"  ]]; then
    echo GREATER
fi
share|improve this answer

You could use the following command to exit with a 0 status if the version extracted is greater than 1.5

${HOME}/temp/.git describe --always --tags HEAD | perl -ne '/v\d+\.(\d+\.\d+)/;exit(1) if $1 <= 1.5'
share|improve this answer
    
Updated to match your new version pattern –  Sylvain Pineau Mar 27 at 11:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.