加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash:将字符串作为整数进行比较

发布时间:2020-12-16 01:07:37 所属栏目:安全 来源:网络整理
导读:我正在尝试测试是否支持Ubuntu版本,如果不支持,那么我在APT文件夹中更新source.list 我知道我不能使用在[[]]中,所以我试过[()],试过[],甚至试图在变量中使用正则表达式和“ – ”,但它不起作用,因为它找不到“file:76” . 我应该如何编写比较工作? 我的代
我正在尝试测试是否支持Ubuntu版本,如果不支持,那么我在APT文件夹中更新source.list

我知道我不能使用<>在[[]]中,所以我试过[()],试过[],甚至试图在变量中使用正则表达式和“ – ”,但它不起作用,因为它找不到“file:76” .

我应该如何编写比较工作?

我的代码:

#!/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
像这样:
[[ $supportLeft -lt 1 || $yearCompare -gt 0 ]]

您可以在man test中找到这些和其他相关的运算符

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读