0

I'm trying to work out how to compare the current openSuSE version number to a preset value.

I've got the current version of the installed OS in $VERSION_ID

I'm now trying to work out how to compare that to '42.3'. So if the value isn't greater than or equal to quit.

if [ ! "$VERSION_ID" -ge 42.3 ]; then
    echo "Sorry Bye";
fi  

I'm getting: [: 42.3: integer expression expected But I don't know how to fix that

Any advise please Thanks

2
  • In general, version numbers aren't floating-point values; they are .-delimited sequences of integers. For instance, 42.10 is almost certainly a newer version than 42.9. Commented Aug 31, 2018 at 13:23
  • @chepner Good point, that dupe would solve the Y part of the XY problem ;) Commented Aug 31, 2018 at 13:53

2 Answers 2

1

You can use a calculator bc:

if [ $(echo "$VERSION_ID<=42.3" |bc -l) -eq "1" ]; then 
    echo "Sorry Bye";
fi
Sign up to request clarification or add additional context in comments.

2 Comments

@Michael O. Thanks this seens to do what I need :)
Slight change so 42.3 and above is allowed: if [ echo $VERSION_ID'<'42.3 | bc -l` != 0 ]; then `
0

Version numbers aren't floating point values; they are .-delimited sequences of integers. 42.27 is newer than 42.3, and 42.2.9 could be a valid version number.

Split the version number into its integer components, and compare them "lexiconumerically":

target=(42 3)
IFS=. read -a v_id <<< "$VERSION_ID"

for ((i=0; i <${#v_id[@]}; i++)); do
  if (( ${v_id[i]} == ${target[i]} )); then
    continue
  fi

  if (( ${v_id[i]} < ${target[i]} )); then
    echo "version < target"
  elif (( ${v_id[i]} > ${target[i]} )); then
    echo "version > target"
  fi
  break
done

3 Comments

You can use v_id[i] and target[i] for arrays in arithmetic context, by the way.
I usually prefer ${...} in arithmetic expressions, as a guard against undefined variables (the variable alone will default to 0, but the empty string from an expansion produces a syntax error).
Good point, too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.