[Solved] Bash - arithmetic comparisons with strings

Need help with C, C++, perl, python, etc?

[Solved] Bash - arithmetic comparisons with strings

Postby Allain » 2017-06-08 02:34

Good morning
I want to share a piece of code which gives me problems when comparing variables
The variables are probably strings and i can not make arithmetic comparisons
As you see i have tried different ways to do it but without success
It seems Bash have problems with decimals ?
So i tried to remove the period n $GSVS and $YYY but with no success

Code: Select all
GSVS2=`echo $GSVS | sed 's/.//g'
YYYY2=`echo $YYY | sed 's/.//g'


Code: Select all
#!/bin/bash

wget -q -O ext "https://extensions.gnome.org/extension-info/?pk=307&shell_version=$GSVS"
GSVS="$(DISPLAY=":0" gnome-shell --version | tr -cd "0-9." | cut -d'.' -f1,2)" #| sed 's/.//g'
XXX=`sed "s/\([0-9]*\.[0-9]*[0-9\.]*\)/\n\1/g" ext | grep "pk" | grep "version" | sed "s/^\([0-9\.]*\).*$/\1/" | sort -V`
YYY=`sed "s/\([0-9]*\.[0-9]*[0-9\.]*\)/\n\1/g" ext | grep "pk" | grep "version" | sed "s/^\([0-9\.]*\).*$/\1/" | sort -V | tail -1`

echo "Gnome-shell version : $GSVS"
echo
echo "Available versions : $XXX"
echo
echo "Last available version : $YYY"

# Tests but not working
#GSVS=`expr $GSVS + 0`
#YYY=`expr $YYY + 0`
#GSVS=$GSVS/.*}
#YYY=$YYY/.*}

if (( $YYY -eq $GSVS )) ; then echo "=" ; fi
if [[ $YYY -gt $GSVS ]] ; then echo ">" ; fi
if test $YYY -le $GSVS ; then echo "<" ; fi


#--- I get those errors

#./comp-versions.sh: line 19: ((: 3.24 -eq 3.14 : syntax error: invalid arithmetic operator (error token is ".24 -eq 3.14 ")
#./comp-versions.sh: line 20: [[: 3.24: syntax error: invalid arithmetic operator (error token is ".24")
#./comp-versions.sh: line 21: test: 3.24: integer expression expected

Which documentations are good about this ?
Last edited by Allain on 2017-06-26 15:13, edited 1 time in total.
Allain
 
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash - arithmetic comparisons with strings

Postby Dai_trying » 2017-06-08 07:55

The first error I can see is you do not have your $GSVS variable set before using it in your wget command.

For comparisons there could be some relevant info on this site might help.

One thing I would try is using quotes inside your comparison, ie
Code: Select all
if (( "$YYY" -eq "$GSVS" )) ; then echo "=" ; fi
Dai_trying
 
Posts: 338
Joined: 2016-01-07 12:25

Re: Bash - arithmetic comparisons with strings

Postby orythem27 » 2017-06-08 10:17

I didn't have to time to read through you code carefully, but bash can only handle integer maths, for floating point values you probably would have to pipe to a higher precision calculator such as bc or dc.

Or see other examples:
https://stackoverflow.com/questions/15224581/floating-point-comparison-with-variable-in-bash
https://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-bash
User avatar
orythem27
 
Posts: 252
Joined: 2017-05-11 07:59
Location: P.R. China

Re: Bash - arithmetic comparisons with strings

Postby Dai_trying » 2017-06-08 12:58

My previous example would not work, you could however try
Code: Select all
if [ "$YYY" == "$GSVS" ] ; then echo "=" ; fi
Dai_trying
 
Posts: 338
Joined: 2016-01-07 12:25

Re: Bash - arithmetic comparisons with strings

Postby dryden » 2017-06-08 18:45

Also this:

Code: Select all
s/.//g


Does not match dots, it matches all characters, and will thus empty the string.
dryden
 
Posts: 60
Joined: 2015-02-04 08:54

Re: Bash - arithmetic comparisons with strings

Postby Allain » 2017-06-10 13:24

I missed again the backslash before the period.
Thanks to point that

I forgot to mention that $GSVS stands for Gnome-shell Version
The solution i found was removing the period with sed and redirect the variable to a file
So when reading the variable from the file i get an integer.

Code: Select all
A="3.14"
B="3.5"

echo "$A" > A
echo "$B" > B
#echo "A=$A - B=$B"
A2=$(cat A | sed 's/\.//g')
B2=$(cat B | sed 's/\.//g')
echo -n "A: " ; echo "$A"
echo -n "B: " ; echo "$B"

if (( $B2 < $A2 )) ; then echo "$B < $A" ; fi
if (( $B2 > $A2 )) ; then echo "$B > $A" ; fi
if (( $B2 == $A2 )) ; then echo "$B = $A" ; fi


Those one work too
Code: Select all
if [ $B2 = $A2 ] ; then echo "B=A - equal" ; fi
if (( $B2 == $A2 )) ; then echo "$B = $A" ; fi

Needs two equal signs in a double parenthesis

Thank you for the tldp link, it help a lot

Command line with BC and DC looks harder. i write my first bash shell script, i will check that later, thank you anyway
Allain
 
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash - arithmetic comparisons with strings

Postby dryden » 2017-06-10 14:33

You don't have to echo it to a file.

You can echo directly to sed:

Code: Select all
A2=$(echo "$A" | sed 's/\.//g')


I think in (( )) you can remove the $ and it will still work.

Your code is not yet fool-proof but comparisons of floats in shell are indeed hard.

One possibility would be:

Code: Select all
A=3.14
B=12.1

diff=$(echo "$A - $B" | bc -l)
case "$diff" in
    -*) // $B was bigger
    0) // was equal
    *) // $A was bigger
esac


but it is all a bit convoluted :p. Regards.
dryden
 
Posts: 60
Joined: 2015-02-04 08:54

Re: Bash - arithmetic comparisons with strings

Postby Allain » 2017-06-18 14:55

I will come to this asap but i am really busy with this first script on Gnome shell extensions
It is my first project in Bash
My previous projects were in Basic, then in Visual Basic Script... a long time ago :-)
This just to thank you about your help and the time you gave me
Other questions will follow since i get new and new ideas with the time going on
And it is sometimes hard to understand english man pages without examples...
Next i will ask you to share my code so i can improve it
But for the time i need to get all my ideas in this script
It needs to do everything i think about

Yes, i am something like perfectionist 8-)
Allain
 
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash - arithmetic comparisons with strings

Postby dryden » 2017-06-18 15:13

Allain wrote:And it is sometimes hard to understand english man pages without examples...


Indeed some man pages seem to actively make it hard to see what they mean by refusing to add examples.

Some people even do this for political reasons: they feel a man page should only describe options, not explain use with examples.

They feel man pages should be reference guides, not the jack-of-all-trades they are being used for, and will actively refuse to make them better.

Makes for poorer systems :(.

Yes, i am something like perfectionist 8-)


That's fine.
dryden
 
Posts: 60
Joined: 2015-02-04 08:54

Re: Bash - arithmetic comparisons with strings

Postby Allain » 2017-06-18 20:23

I saw this dryden
Man pages are not easy for newbies
I use Linux for one year only
I find my way but it is hard
So my next question will follow soon
Allain
 
Posts: 20
Joined: 2016-12-06 13:55


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable