【问题标题】:Bash: comparing a string as an integerBash:将字符串作为整数进行比较
【发布时间】: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


【解决方案1】:

BaSH 条件句 - 当涉及到数字和算术时 - 非常令人困惑。

这两种方法都可以:

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

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

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

【讨论】:

    【解决方案2】:

    像这样:

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

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

    【讨论】:

    • 0]] 之间需要一个空格
    • 啊,是的...我在复制粘贴时忽略了这一点。谢谢!
    • :( 与“if (( $supportLeft 0 ))”相同的错误...
    • 像这样工作:[[ "$supportLeft" -lt 1 ] || [ "$yearCompare" -gt 0 ]] ...谢谢大家:)
    • 除非这是 bash 4 中的新内容,否则我很确定语法是完全错误的......你不能像这样将 [[] 配对......
    【解决方案3】:

    不确定这是否有帮助,但是当我搜索“将字符串与 bash 中的 int 进行比较”时,Google 中的这个问题很高

    您可以通过添加 0 将字符串“转换”为 bash 中的 int

    NUM="99"
    NUM=$(($NUM+0))
    

    如果您还必须处理 NULL,这将非常有用

    NUM=""
    NUM=$(($NUM+0))
    

    确保字符串中没有空格!

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

    (在 Solaris 10 上测试)

    【讨论】:

      【解决方案4】:

      这似乎有效:

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

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

      【讨论】:

      • :( 尝试过,但它以 3 个通知结尾(我猜):无法打开 1,找不到文件;找不到 993;找不到 72;.. 看起来它与 $yearsCompare 有关=> 72 , 1 到 1 和 993 到 $supportLeft 。今天之前有过类似的事情......对此有任何想法吗??
      猜你喜欢
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 2018-03-17
      • 2021-03-08
      • 2012-05-12
      • 1970-01-01
      相关资源
      最近更新 更多