主要有两种方法:

使用if判断

read -p "Input a number(0-5): " num
array=(0 1 2 3 4 5)
if [[ "${array[*]}"  =~ "${num}" ]]; then
echo "You select $num"
else
echo "Invalid input!"
exit 1
fi

使用case判断

read -p "Input a number(0-5): " num
case $num in
[0-5])
echo "You select $num"
;;
*)
echo "Invalid input!"
exit 1
;;
esac

相关文章: