Bash: string comparison as a whole

I am trying to check if the version of Ubuntu is supported or not, and in case it is not, I update source.list in the APT folder

I know that I can’t use <> inside [[ ]] , so I tried [( )] , tried [] , and even tried using regexp, there is a β€œ-” in the variable, but this does not work because it doesn’t could find "file: 76".

How do I write a comparison to work?

My code is:

 #!/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 
+7
source share
3 answers

Like this:

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

You can find these and other related statements in man test

+7
source

It works:

 if (( $supportLeft < 1 )) || (( $yearCompare > 0 )) 

or

 if (( $supportLeft < 1 || $yearCompare > 0 )) 
+4
source

Not sure if this is any help, but this question was high on google when I was looking for "compare string with int in bash"

You can "distinguish" a string from int in bash by adding 0

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

This works great if you also need to deal with NULL

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

Make sure there are no spaces in the line!

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

(Tested on Solaris 10)

+3
source

All Articles