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
精彩评论