Shell判断数值是否存在于列表

主要有两种方法:

使用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
原文地址:https://www.cnblogs.com/azureology/p/13223679.html