0

I am trying to compare to build numbers and echo which is greater. Here is a script i wrote

    New_Cycle_Num='c4.10'
    Old_Cycle_Num='c4.9'
    if  [ "$New_Cycle_Num" == "$Old_Cycle_Num" ];
    then echo 'both are equal'
    elif [ "$New_Cycle_Num" "<" "$Old_Cycle_Num" ]];
    then echo 'New_Cycle_Num is less than Old_Cycle_Num'
    else echo 'New_Cycle_Num is greater than Old_Cycle_Num'
    fi

My script gives me ioutput as 'New_Cycle_Num is less than Old_Cycle_Num" instead of last statement. why is c4.10 compared to be less than c4.9? any help to correct this ?? Many thanks!!

3
  • 1
    In math 4.1 is less than 4.9 Commented Feb 10, 2014 at 23:48
  • It's not doing floating point comparison or even integer comparison, but string comparison. Commented Feb 10, 2014 at 23:52
  • but 10 is greater than 9 right. so how do I write my script so i get c4.10 is greater than c4.9 or similar case? Commented Feb 11, 2014 at 0:22

1 Answer 1

1

You get the result you get because with lexical comparison, comparing the 4th character, "1" appears before "9" in the dictionary (in the same sense that "foobar" would appear before "food", even though "foobar" is longer).

Tools like ls and sort have a "version sorting" option, which will be useful here, albeit somewhat awkward:

New_Cycle_Num='c4.10'
Old_Cycle_Num='c4.9'
if [[ $New_Cycle_Num == $Old_Cycle_Num ]]; then
    echo 'equal'
else
    before=$(printf "%s\n" "$New_Cycle_Num" "$Old_Cycle_Num")
    sorted=$(sort -V <<<"$before")
    if [[ $before == $sorted ]]; then
        echo 'New_Cycle_Num is less than Old_Cycle_Num'
    else
        echo 'New_Cycle_Num is greater than Old_Cycle_Num'
    fi
fi
New_Cycle_Num is greater than Old_Cycle_Num

I can't think of a great alternative. There might be

echo -e "c4.10\nc4.9" | 
perl -MSort::Versions -E '
  $a=<>; $b=<>; chomp($a, $b); $c=versioncmp($a,$b);
  say "$a is ". ($c==0 ? "equal to" : $c < 0 ? "less than" : "greater than") . " $b"
'
c4.10 is greater than c4.9

But you have to install Sort::Versions from CPAN.

Sign up to request clarification or add additional context in comments.

4 Comments

yes, this works thank you. but as you said its little complicated. I tried few other numbers and each time it returns what is expected. hope there is much simpler command for attaining this!
It's not simple; see this previous question. Since you also have a letter in there, it's even more difficult (unless the "c" is constant, and can just be removed).
THanks Gordon. yes, c is constant and probably can be removed. so, in that case, how could the script be comparing between 4.10 to 4.9?
Glenn - thank you, I will try to install Sort versions. but anyway I implemented the first scenario and it works now. Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.