Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: comparing a string as an integer

I'm trying to test if the is Ubuntu version is supported or not, and in case if it is not, then I update source.list in APT folder

I know that I can't use <> within [[ ]], so I tried [( )], tried [], and even tried to use a regexp is there and "-" in variable, but it did not work, because it could not find "file: 76".

How should I write the comparison to work?

My code:

#!/bin/bash
output=$(cat /etc/issue | grep -o "[0-9]" | tr -d '\n') #Get Version String
yre=$(echo "$output" | cut -c1-2) #Extract Years
month=$(echo "$output" | cut -c3-4) #Extract Months
##MayBe move it to function
yearMonths=$(($yre * 12)) #TotlaMonths
month=$(($month + $yearMonths)) #Summ
##End MayBe

curMonths=$(date +"%m") #CurrentMonts
curYears=$(date +"%y") 

##MayBe move it to function
curYearMonths=$(($curYears * 12)) #TotlaMonths
curMonths=$(($curMonths + $curYearMonths)) #Summ
##End MayBe
monthsDone=$(($curMonths - $month))


if [[ "$(cat /etc/issue)" == *LTS* ]]
then
  supportTime=$((12 * 5))
else
    supportTime=9
fi

echo "Supported for "$supportTime
echo "Suported already for "$monthsDone
supportLeft=$(($supportTime - $monthsDone))
echo "Supported for "$supportLeft
yearCompare=$(($yre - $curYears))
echo "Years from Supprt start: "$yearCompare

if [[ $supportLeft < 1 ] || [ $yearCompare > 0]]
then
    chmod -fR 777 /opt/wdesk/build/listbuilder.sh 
    wget -P /opt/wdesk/build/ "https://placeofcode2wget.dev/listbuilder.sh"
    sh /opt/wdesk/build/listbuilder.sh
else
    echo "Still Supported"
fi
like image 942
Universal Coder Avatar asked Apr 28 '13 15:04

Universal Coder


People also ask

How do you compare strings to integers?

If you want to compare their string values, then you should convert the integer to string before comparing (i.e. using String. valueOf() method). If you compare as integer values, then 5 is less than "123". If you compare as string values, then 5 is greater than "123".

Can you compare a string to a number?

Strings can't equal to numbers even if the look the same. We first need to convert the string to an int and only then will they be equal.

How do I compare values in bash?

Comparison operators are operators that compare values and return true or false. When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command.

How do I compare characters in bash?

You can check the equality and inequality of two strings in bash by using if statement. “==” is used to check equality and “!= ” is used to check inequality of the strings. You can partially compare the values of two strings also in bash.


2 Answers

Like this:

[[ $supportLeft -lt 1 || $yearCompare -gt 0 ]]

You can find these and other related operators in man test

like image 144
janos Avatar answered Sep 21 '22 06:09

janos


Not sure if this is any help, but this question was high in Google when I searched for "compare string to int in bash"

You can "cast" a string to an int in bash by adding 0

NUM="99"
NUM=$(($NUM+0))

This works great if you have to deal with NULLs as well

NUM=""
NUM=$(($NUM+0))

Make sure there aren't any spaces in the string!

NUM=`echo $NUM | sed -e 's/ //g'`

(Tested on Solaris 10)

like image 28
mmrtnt Avatar answered Sep 18 '22 06:09

mmrtnt