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

Tom
  • 1,436
  • 24
  • 50
  • 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`. – chepner Aug 31 '18 at 13:23
  • @chepner Good point, that dupe would solve the Y part of the XY problem ;) – Benjamin W. Aug 31 '18 at 13:53

2 Answers2

1

You can use a calculator bc:

if [ $(echo "$VERSION_ID<=42.3" |bc -l) -eq "1" ]; then 
    echo "Sorry Bye";
fi
Michael
  • 5,095
  • 2
  • 13
  • 35
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
chepner
  • 497,756
  • 71
  • 530
  • 681