大于比较会导致Bash中的结果不一致

时间:2019-02-27 01:49:14

标签: bash string-comparison

此脚本似乎给出不一致的结果。例如,当if语句看到其第一个较大的字符串时,它可以正常工作。但是,有时较大的后来的字符串会被完全忽略:

ITEM[0]="XX"
ITEM[1]="XXXXXXX"
ITEM[2]="X"
ITEM[3]="XXXXXXXXXXXX"
ITEM[4]="XXXX"

SETPOINT=0
for i in "${!ITEM[@]}"; do
        STRING="${ITEM[$i]}"
        LENGTH=${#STRING}
        echo "String length = $LENGTH"
        if [ $LENGTH \> $SETPOINT ]; then
                SETPOINT=$LENGTH
                echo "Setpoint was updated to $SETPOINT"
        fi
        echo "Loop again"
done
echo "Final setpoint = $SETPOINT"

这是示例输出:

String length = 2
Setpoint was updated to 2
Loop again

String length = 7
Setpoint was updated to 7
Loop again

String length = 1
Loop again

String length = 12 <--- Why didn't it catch this one?????
Loop again

String length = 4
Loop again

Final setpoint = 7

此外,最初我尝试在if语句中进行变量扩展和字符串计数,因此我不必创建“ STRING”和“ LENGTH”,但是我无法弄清楚语法在if内同时扩展数组变量并同时计算字符串。因此,如果您也为了缩短代码而对此有所考虑,那就太好了!

谢谢!

1 个答案:

答案 0 :(得分:1)

glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, dev->atlas_width, dev->atlas_height, dev->atlas_depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, dev->atlas_offset_x, dev->atlas_offset_y, dev->atlas_offset_z, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image);替换为\>

-gt解释说:

man test