Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Solved] Bash - arithmetic comparisons with strings

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Allain
Posts: 20
Joined: 2016-12-06 13:55

[Solved] Bash - arithmetic comparisons with strings

#1 Post by Allain »

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.

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Bash - arithmetic comparisons with strings

#2 Post by Dai_trying »

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

User avatar
orythem27
Posts: 252
Joined: 2017-05-11 07:59
Location: P.R. China

Re: Bash - arithmetic comparisons with strings

#3 Post by orythem27 »

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/152 ... le-in-bash
https://stackoverflow.com/questions/865 ... rs-in-bash

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Bash - arithmetic comparisons with strings

#4 Post by Dai_trying »

My previous example would not work, you could however try

Code: Select all

if [ "$YYY" == "$GSVS" ] ; then echo "=" ; fi

dryden
Posts: 80
Joined: 2015-02-04 08:54

Re: Bash - arithmetic comparisons with strings

#5 Post by dryden »

Also this:

Code: Select all

s/.//g
Does not match dots, it matches all characters, and will thus empty the string.

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash - arithmetic comparisons with strings

#6 Post by Allain »

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

dryden
Posts: 80
Joined: 2015-02-04 08:54

Re: Bash - arithmetic comparisons with strings

#7 Post by dryden »

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.

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash - arithmetic comparisons with strings

#8 Post by Allain »

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-)

dryden
Posts: 80
Joined: 2015-02-04 08:54

Re: Bash - arithmetic comparisons with strings

#9 Post by dryden »

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.

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash - arithmetic comparisons with strings

#10 Post by Allain »

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

Post Reply