【发布时间】:2013-04-22 06:33:14
【问题描述】:
我正在尝试测试是否支持 Ubuntu 版本,如果不支持,则更新 APT 文件夹中的 source.list
我知道我不能在[[ ]] 中使用<>,所以我尝试了[( )],尝试了[],甚至尝试在变量中使用正则表达式和“-”,但确实如此不起作用,因为它找不到“文件: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
【问题讨论】:
-
顺便说一句,要避免那里的UUCA,请尝试
output=$(grep -o "[0-9]" /etc/issue)(是的,tr在这里也是完全多余的)。我猜你也应该grep不止一个数字? -
实际上,从
lsb_release获取机器可读版本比尝试解析/etc/issue更简单、更可靠。 -
@tripleee 可能,正如我所说的我是 bewbie ,所以谢谢你的警告(关于 UUCA)。 lsb_release 发出了一些警告消息,所以我跳过了它,但我想我会重新考虑。
标签: string bash if-statement compare