0

I have a string that represents a kind of version. I would like to increment it and if the second part exceeds 9 to go to the next higher number The following seems to work. I was wondering if there is a more succinct way to do this

FIRST=$(echo -n "$VERSION"| cut -d "." -f 1)  
SECOND=$(echo -n "$VERSION"|cut -d "." -f 2)  
SECOND=$((SECOND + 1))  
if [[ $SECOND -ge 10 ]]; then  
    SECOND="0"  

fi  
FIRST=$(($FIRST + 1))

NEW_NUMBER=$FIRST.$SECOND  

Examples:

Input: 102.8 Output: 102.9     
Input: 102.9 Output: 103.0  
  • A change in the first part of the version number does not correspond to a new major release? – Kusalananda Mar 28 '18 at 10:22
  • @Kusalananda: yes but why? – Jim Mar 28 '18 at 10:24
1
printf '%s + 0.1\n' "$VERSION" | bc

This adds 0.1 to the floating point number in $VERSION.

Testing:

$ VERSION=10.2
$ printf '%s + 0.1\n' "$VERSION" | bc
10.3

$ VERSION=10.9
$ printf '%s + 0.1\n' "$VERSION" | bc
11.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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