if [[ $entriestoDisp == [^1-9] ]]; then
echo "Invalid number of entries, choose between 1 to 9."
else
#displays logs
嗨,我在这段特殊的代码上遇到了麻烦。我想在继续之前验证变量entrytoDisp的值在1到9之间。我在做什么错了?
答案 0 :(得分:3)
请勿使用正则表达式进行范围检查,除非您要在继续操作之前验证entriesToDisp
确实是整数。只需使用常规比较运算符即可。
if ! [[ $entriesToDisp =~ [[:digit:]]+ ]]; then
echo "$entriesToDisp is not an integer"
elif ! (( 1 <= $entriesToDisp && $entriesToDisp <= 9 )); then
echo "$entriesToDisp not in range 1-9"
fi
无论涉及多少位数,这都将起作用。
答案 1 :(得分:0)
您非常接近:
if [[ ! $entriestoDisp = [1-9] ]]; then
echo ouch
fi