【发布时间】:2017-12-17 02:22:39
【问题描述】:
我正在自学 bash
我怎样才能做到这一点?
有三个检查点...没有空行,没有特殊字符和 没有小于 1 和大于最大数的数
然而,在第三个检查点,如果你尝试输入一个特殊字符,整个事情就会像纸牌屋一样翻滚。
如何确保用户没有输入空行、没有特殊字符以及没有小于 1 和大于预定最大数字的数字?
read -p "请输入行号:" line_number
# First checkpoint
# No blank lines accepted as input
while [[ -z "$line_number" ]] ;
do
echo
echo "Line number can not be blank."
echo
read -p "Please enter a valid line number? " line_number
echo
done
# Second checkpoint
# No special characters allowed as input
# Escaping the backtick or accent grave requires three back slashes before the backtick
while [[ $line_number == *['!'@#\$%^\&*()_+?~-\"\\\`]* ]] ;
do
echo
echo "No special characters allowed"
echo
read -p "Please enter a valid line number? " line_number
echo
done
# Third checkpoint
# No number less than 1 and greater than the $total_line_number
while [[ $line_number -lt 1 || $line_number -gt $total_line_number ]] ;
do
echo
echo -e "Line number can not be lesser than 1$ and bigger than $total_line_number."
echo
read -p "Please enter a number between the 1 and $total_line_number scope? " line_number
echo
done
【问题讨论】:
标签: bash loops while-loop