如果条件不满意的结果

时间:2016-05-22 20:12:57

标签: unix ksh aix

下面是我正在应用以下unix脚本的文件。

    0.30 2.30 - - - - - - -
    2.30 3.30 - - NA NA - - -
    3.30 4.30 - - NA NA - - -
    4.30 5.30 - - - - - - -
    5.30 6.30 - - NA NA - - -
    6.30 0.30 - - - - - - -

##Time=`echo $(date) | awk -F ' ' '{print $4}' | awk -F ':' '{print $1}'` 
Time=05;
Result="False";
while [[ "${Result}" != "True" ]]
do
    while read -r f1 f2 f3 f4 f5 f6 f7 f8 f9
    do
        if [[ "${Time}" -ge "${f1}" && "${Time}" -le "${f2}" ]]
        then 
                echo $Time is between $f1 $f2 True
                Result="True";
        else
                echo $Time is between $f1 $f2 False
        fi
    done < consolidated.txt
done

我得到以下结果:请注意if条件满足两次。 05不在5.30和6.30之间,它仍然返回true。 我在这里错过了什么。

05 is between 0.30 2.30 False
05 is between 2.30 3.30 False
05 is between 3.30 4.30 False
05 is between 4.30 5.30 True
05 is between 5.30 6.30 True
05 is between 6.30 0.30 False

2 个答案:

答案 0 :(得分:1)

您遇到的问题是由于行Time=05; ksh未将此解释为浮点值。 let会迫使它这样做。

$ Time=05;echo $Time
05
$ let Time=05;echo $Time
5

另一个常见的习语是Time=$(echo $Time |bc)

以下是您逻辑的另一种措辞:

$echo $Time $f1 $f2
05 5.30 6.30
$
$let Time=05     
$ if [[ (( $Time > $f1 )) && (( $Time < $f2 )) ]]
> then 
>        echo $Time is between $f1 $f2 True
> else
>        echo $Time is between $f1 $f2 False
>fi    
5 is between 5.30 6.30 False

(( ... ))是与[[ ... ]]类似的数字条件 它旨在用于ifwhile构造。

您可以在学习Korn Shell的6.2.2节中找到更多详细信息here

答案 1 :(得分:1)

可见各种问题:

  • 代码中的外部循环存在问题。如果consolidated.txt中的所有时间范围都不匹配,${Result}仍为False且外部循环永不终止。
  • 数据中的最后一个时间范围6.30 0.30永远不会匹配任何内容,因为范围的结尾在数值上小于范围的开头。
  • 使用time >= start && time <= end条件时,如果时间值为5.30,则该值将匹配4.30 5.30范围(等于结束时间)和{{ 1}}范围(等于开始时间)。

与Bash不同,Korn shell支持浮点运算,这很有用。

而不是5.30 6.30并使用字符串比较([[ … ]]-ge),您应该使用-le和(浮点)数字比较((( … ))>=)。

将这些更改组合到一个脚本中,并忽略<变量,因为它与此处不相关,您得到:

Result

是的,此处文档末尾的#!/bin/ksh for Time in 0 3 05 5.30 7.00 do while read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 do if (( "${Time}" >= "${f1}" && "${Time}" < "${f2}" )) then echo "${Time} is between ${f1} and ${f2} True" else echo "${Time} is between ${f1} and ${f2} False" fi done << EOF 0.30 2.30 - - - - - - - 2.30 3.30 - - NA NA - - - 3.30 4.30 - - NA NA - - - 4.30 5.30 - - - - - - - 5.30 6.30 - - NA NA - - - 6.30 24.00 - - - - - - - EOF done 确实需要位于左边距。您可以使用EOF并缩进结束标记<<-'EOF',只要缩进是制表符,而不是空格。

此脚本的输出为:

EOF

如果您将0 is between 0.30 and 2.30 False 0 is between 2.30 and 3.30 False 0 is between 3.30 and 4.30 False 0 is between 4.30 and 5.30 False 0 is between 5.30 and 6.30 False 0 is between 6.30 and 24.00 False 3 is between 0.30 and 2.30 False 3 is between 2.30 and 3.30 True 3 is between 3.30 and 4.30 False 3 is between 4.30 and 5.30 False 3 is between 5.30 and 6.30 False 3 is between 6.30 and 24.00 False 05 is between 0.30 and 2.30 False 05 is between 2.30 and 3.30 False 05 is between 3.30 and 4.30 False 05 is between 4.30 and 5.30 True 05 is between 5.30 and 6.30 False 05 is between 6.30 and 24.00 False 5.30 is between 0.30 and 2.30 False 5.30 is between 2.30 and 3.30 False 5.30 is between 3.30 and 4.30 False 5.30 is between 4.30 and 5.30 False 5.30 is between 5.30 and 6.30 True 5.30 is between 6.30 and 24.00 False 7.00 is between 0.30 and 2.30 False 7.00 is between 2.30 and 3.30 False 7.00 is between 3.30 and 4.30 False 7.00 is between 4.30 and 5.30 False 7.00 is between 5.30 and 6.30 False 7.00 is between 6.30 and 24.00 True 条件更改为<,这等同于问题所用的条件,那么您将获得输出:

<=

将最后一个范围保留为… 5.30 is between 3.30 and 4.30 False 5.30 is between 4.30 and 5.30 True 5.30 is between 5.30 and 6.30 True 5.30 is between 6.30 and 24.00 False … ,输出的最后部分如下所示:

6.30 0.30