小编Uni*_*der的帖子

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

我正在尝试测试是否支持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 …
Run Code Online (Sandbox Code Playgroud)

string bash if-statement compare

12
推荐指数
3
解决办法
4万
查看次数

标签 统计

bash ×1

compare ×1

if-statement ×1

string ×1