Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Bash:将字符串作为整数进行比较_String_Bash_If Statement_Compare - Fatal编程技术网

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

String Bash:将字符串作为整数进行比较,string,bash,if-statement,compare,String,Bash,If Statement,Compare,我正在尝试测试是否支持is Ubuntu版本,如果不支持,则更新APT文件夹中的source.list 我知道我不能在[[]]中使用,所以我尝试了[()],尝试了[],甚至尝试了在变量中使用regexp和“-”,但它不起作用,因为它找不到“file:76” 我应该如何写比较才能工作 我的代码: #!/bin/bash output=$(cat /etc/issue | grep -o "[0-9]" | tr -d '\n') #Get Version String yre=$(echo "$o

我正在尝试测试是否支持is Ubuntu版本,如果不支持,则更新APT文件夹中的source.list

我知道我不能在
[[]]
中使用
,所以我尝试了
[()]
,尝试了
[]
,甚至尝试了在变量中使用regexp和“-”,但它不起作用,因为它找不到“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
#/bin/bash
输出=$(cat/etc/issue | grep-o“[0-9]”| tr-d'\n')|获取版本字符串
yre=$(回声“$输出”|切割-c1-2)#提取年份
月份=$(回显“$输出”|切割-c3-4)#提取月份
##也许把它移到功能上
年月数=$($yre*12))#至月数
月=$($月+$年月))#总和
##也许会结束
curMonths=$(日期+%m)#CurrentMonts
当前年份=$(日期+%y)
##也许把它移到功能上
curYearMonths=$($curYears*12))#totlammonths
curMonths=$($curMonths+$curYearMonths))#总和
##也许会结束
monthsDone=$($curMonths-$month))
如果[[“$(cat/etc/issue)”===*LTS*]]
然后
支持时间=$((12*5))
其他的
支持时间=9
fi
echo“受支持的时间”$supportTime
echo“已支持”$monthstone
supportLeft=$($supportTime-$monthsDone))
echo为$supportLeft提供“支持”
年份比较=$($yre-$curYears))
echo“从支持开始算起的年份:$yearCompare
如果[[$supportLeft<1]| |[$yearCompare>0]]
然后
chmod-fR 777/opt/wdesk/build/listbuilder.sh
wget-P/opt/wdesk/build/”https://placeofcode2wget.dev/listbuilder.sh"
sh/opt/wdesk/build/listbuilder.sh
其他的
echo“仍受支持”
fi
这似乎有效:

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

if($supportLeft<1 | |$yearCompare>0))
像这样:

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

你可以在
mantest
中找到这些运算符和其他相关运算符,不确定这是否有帮助,但当我搜索“在bash中将字符串与int进行比较”时,这个问题在Google中很重要

通过添加0,可以在bash中将字符串“强制转换”为int

NUM="99"
NUM=$(($NUM+0))
如果您还必须处理空值,这将非常有效

NUM=""
NUM=$(($NUM+0))
确保字符串中没有空格

NUM=`echo $NUM | sed -e 's/ //g'`

(在Solaris 10上测试)

BaSH条件在数字和算术方面非常混乱

这两种方法中的任何一种都有效:

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

if((支持左<1 | |年比较>0))

注意这两种方法都将空值视为零。根据您的脚本和环境,这可能是有利的,因为如果等式两边的变量值为空,它们不会生成错误消息。

作为一种旁白,为了避免出现错误,请尝试
output=$(grep-o“[0-9]”/etc/issue)
(是的,
tr
在这里也是完全多余的)。我想,你可能也应该
grep
来获取一个以上的数字?事实上,从中获取一个机器可读的版本比尝试解析
/etc/issue
@tripleee可能要简单得多,因为我说过我是bewbie,所以感谢你的警告(关于UUCA)。lsb_发布了一些警告消息,所以我跳过了它,但我想我会重新考虑。你需要在
0
]
之间留出一个空格啊,是的。。。我在粘贴副本时忽略了这一点。谢谢!:(与我在“if($supportLeft<1 | |$yearCompare>0)”中遇到的错误相同)……工作方式如下:[[“$supportLeft”-lt 1]|[“$yearCompare”-gt 0]……谢谢大家:)除非这是
bash
4中的新内容,否则我很确定语法是完全错误的。。。你不能像这样把
[[/code>和
]
配对…:(尝试过,但最后有3个通知(我猜):无法打开1,文件找不到;993找不到;72找不到;。看起来它与$yearsCompare=>72,1对1和993对$supportLeft有关。今天以前有过类似的事情吗?你知道吗??
if [ $((supportLeft)) -lt 1 ] || [ $((yearCompare)) -gt 0 ]
if (( supportLeft < 1 || yearCompare > 0 ))