我正在编写Bash脚本,要求您只输入数字。如何防止在输入数字时显示非数字?例如,如果我在提示符下键入123ab45c6
,则屏幕上只会显示123456
。
答案 0 :(得分:2)
#/bin/bash
echo "Please enter a number"
# variable to store the input
number=""
# reading in silent mode character by character
while read -s -n 1 c
do
case $c in
[0-9])
# if the read character is digit add it to the number and print the number
number="${number}${c}"
echo -en "\r${number}"
;;
'')
# break on ENTER
echo
break;;
esac
done
echo "You entered a number ${number}"