开发者

How do I check if a number entered is between 1 and 31

I am trying to capture the day of the month from the user 开发者_高级运维and want to check if the day is valid - between 1 and 31. I don't want the user to have to add a leading 0 to single digit days. How do I accomplish this check in a bash if statement?

Thanks.


This is not a job for a regular expression!

This is a job for a conditional expression.

Here's some code to get you started.

if [[ $number -lt 31 ]]; then 
    echo $number is less than 31
else 
    echo $number is greater than 31
fi

Of course, date validation is far more complex than this. How many days are there in February?


Use the right tool for the job. Regex is not the right tool for this job.


Just do a numeric comparison:

if (( var >= 1 && var <= 31))
then
    echo "Valid"
fi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜