【发布时间】:2014-03-16 07:50:00
【问题描述】:
假设用户喂食时间:
read -p "Enter the start time you want to start(hh:mm:ss) " user_start_time
和正则表达式:
timePattern="[0-2][0-3]:[0-5][0-9]:[0-5][0-9]"
如果我这样做:
if [ "$user_start_time" == "$timePattern" ]
#or if [ "$user_start_time" =~ "$timePattern" ]
then
echo "in if"
else
echo "in else"
fi
它不验证.....作为新手,我了解int 或string 的比较,但我们如何处理time(日期也在那里,但date -d 解决了很多问题问题)
There is this question too 但无法理解接受的答案!!
【问题讨论】:
-
您的问题是在
[ ... ]中使用==和=~。您需要使用双方括号。请参阅链接的问题。 -
@devnull :是的,在你发布的那一刻就提到了它......谢谢伙计! :)
-
@devnull :一个简单的问题,如果我总是在验证中使用
[[而不是[只是为了更安全,这会是一个好习惯吗?? -
此外,避免使用正则表达式来验证日期/时间。您可能需要参考 this 答案以获取替代方法。
-
可以,只是
[[不那么便携!如果你确定你的脚本会在哪里运行,我认为没有什么坏处。
标签: regex bash shell time-format