【发布时间】:2022-10-23 03:12:35
【问题描述】:
我有带有 If 条件的 For 循环,但它不能正常工作。 它假设检查每个索引值,如果 < 255 则显示有效,否则无效。 第三和第四不正确。
如何解决这个问题?
listNumber=(25 255 34 55)
listLength=${#listNumber[@]}
isValid=0
for ((index=0; index<$listLength; index++)); do
itemNumber="$((index+1))"
if [[ ${listNumber[$index]} < 255 ]]; then
echo -e "Item $itemNumber : ${listNumber[$index]} is Valid. \n"
isValid=1
else
echo -e "Item $itemNumber : ${listNumber[$index]} is NOT Valid. \n"
fi
done
Result:
Item 1 : 25 is Valid.
Item 2 : 255 is NOT Valid.
Item 3 : 34 is NOT Valid.
Item 4 : 55 is NOT Valid.
【问题讨论】: