Home > front end >  How to know if decimal number is major to another?
How to know if decimal number is major to another?

Time:09-17

EDIT/UPDATE: Solutioned! Check how I did this scrolling down.

I'm trying to create an auto-update script, but the problem is that the only mode I can do it is removing the dots from the version number, what can (and probablly will) give an error on the future.

The problem is that I can't use numbers with dot/decimal numbers with "(( $num > $num2 ))". This is a very specific problem :/

The actual script is this:

VER=$(curl --silent "https://api.github.com/repos/user/repository/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' | tr -d .)
INSTALLVER=$(cat $HOME/my-path/example-program-version.txt | tr -d .)
if (( $VER > $INSTALLVER )); then
echo need_update
yes "i" | sudo myinstallscript example
else
echo already_updated
fi

(I get the $INSTALLVER number with "myinstallscript")

If you can help me, very thanks! Also, very thanks for reading!

CodePudding user response:

@KamilCuk Thanks! Problem solved. I picked a reply from the link that you sent, and I adapted this putting the "head" command and putting this inside the variable.

Solution:

LATESTVER=`echo -e "$VER\n$INSTALLVER" | sort -V -r | head -n 1`

(Echo the two variables with echo, one in each line, then sort them putting the major on the first line with sort, getting just the first line with head, and putting this on the "LATESTVER" variable)

  • Related