更新时间:2022-06-03 05:48:21
如果您使用 Bash,***使用算术表达式 ((...))
以提高可读性和灵活性:
If you are using Bash, you are better off using the arithmetic expression, ((...))
for readability and flexibility:
if ((number >= 2 && number <= 5)); then
# your code
fi
循环读取直到输入有效数字:
To read in a loop until a valid number is entered:
#!/bin/bash
while :; do
read -p "Enter a number between 2 and 5: " number
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
if ((number >= 2 && number <= 5)); then
echo "valid number"
break
else
echo "number out of range, try again"
fi
done
((number >= 2 && number 也可以写成
((2 .
另见: