简体   繁体   English

Bash:将字符串比较为整数

[英]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我正在尝试测试是否支持 Ubuntu 版本,如果不支持,则更新 APT 文件夹中的 source.list

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".我知道我不能在[[ ]]使用<> ,所以我尝试了[( )] ,尝试了[] ,甚至尝试使用正则表达式和变量中的“-”,但它没有用,因为它找不到“文件: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 this:像这样:

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

You can find these and other related operators in man test你可以在man test找到这些和其他相关的操作符

Not sure if this is any help, but this question was high in Google when I searched for "compare string to int in bash"不确定这是否有帮助,但是当我搜索“将字符串与 bash 中的 int 进行比较”时,这个问题在 Google 中很高

You can "cast" a string to an int in bash by adding 0您可以通过添加 0 将字符串“强制转换”为 bash 中的 int

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

This works great if you have to deal with NULLs as well如果您还必须处理 NULL,这很有效

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) (在 Solaris 10 上测试)

This seems to work:这似乎有效:

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

or要么

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

BaSH conditionals are - when it comes to numbers and arithmetic - terribly confusing. BaSH 条件 - 当涉及到数字和算术时 - 非常令人困惑。

Either of these methods will work:这些方法中的任何一种都有效:

if [ $((supportLeft)) -lt 1 ] || [ $((yearCompare)) -gt 0 ]

or要么

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

Note both methods treat null values as zero.请注意,这两种方法都将空值视为零。 Depending on your script and environment, this may be advantageous in the sense they won't generate an error message if the value of the variable on either side of the equation is null.根据您的脚本和环境,这可能是有利的,因为如果等式任一侧的变量值为空,它们就不会生成错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM