我有一个脚本,询问用户以下问题:
read -p "Input time duration of simulation in HH:MM:SS format" -e Time
现在我如何确保用户输入$ Time的正确表格 在实际使用变量$ Time之前?
答案 0 :(得分:2)
我假设您需要检查小时< 24分钟< 60秒,< 60?
read -p "Input time duration of simulation in HH:MM:SS format " -e Time
while :; do
if [[ $Time =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then
if (( BASH_REMATCH[3] < 60 ))\
&& (( BASH_REMATCH[2] < 60 ))\
&& (( BASH_REMATCH[1] < 24 )); then
break
fi
fi
read -p "Wrong format. Please use the HH:MM:SS format " -e Time
done
# Do something with $Time
答案 1 :(得分:0)
你可以这样测试:
if [[ $Time =~ ^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$ ]]; then
echo "format is ok"
else
echo >&2 "format is wrong"
fi