我正在尝试测试是否支持Ubuntu版本,如果不支持,则更新APT文件夹中的source.list
我知道我不能在[[ ]]中使用<>,所以我尝试了[( )],尝试了[],甚至尝试在变量中使用regexp is there和"-“,但它不起作用,因为它找不到"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发布于 2013-04-28 23:50:45
如下所示:
[[ $supportLeft -lt 1 || $yearCompare -gt 0 ]]您可以在man test中找到这些运算符和其他相关运算符
发布于 2013-06-14 01:30:39
我不确定这是不是有帮助,但当我在Google中搜索"compare string to int in bash“时,这个问题在Google中很高。
在bash中,您可以通过添加0将字符串“强制转换”为int类型
NUM="99"
NUM=$(($NUM+0))如果您还必须处理NULL,则此方法非常有效
NUM=""
NUM=$(($NUM+0))确保字符串中没有任何空格!
NUM=`echo $NUM | sed -e 's/ //g'`(在Solaris 10上测试)
发布于 2013-04-28 23:48:43
这似乎起作用了:
if (( $supportLeft < 1 )) || (( $yearCompare > 0 ))或
if (( $supportLeft < 1 || $yearCompare > 0 ))https://stackoverflow.com/questions/16264311
复制相似问题