-2
#!/bin/bash hour="hour" #variable which holds the string for hour min="min" #variable which holds the string for min minValue="4" hourValue="4" hourEntered="4" minEntered="4" ########### # hour # ########### if [ ! "$hourEntered" ]; then hourValue=$hourValue echo "you did not enter an hour" #if the user did input the correct values take them over #value to be entered 0 or 23.... elif [[ "$hourEntered" =~ ^[0-23]+$ ]]; then hourValue=$hourEntered echo "you entered for hourEntered:$hourEntered" #else provide a message and provide help.... else #set the read out values hourValue=$hourValue fi ########### # min # ########### #check if a value was entered if not take the read out values over if [ ! "$minEntered" ]; then minValue=$minValue echo "you did not enter an min" #if the user did input the correct values take them over #value to be entered 0 or 60.... elif [[ "$minEntered" =~ ^[0-59]+$ ]]; then minValue=$minEntered echo "you entered for minEntered:$minEntered" #else provide a message and provide help.... else #set the read out values minValue=$minValue fi 

So there is something weird going on, apparently the statement

[[ "$hourEntered" =~ ^[0-23]+$ ]]; 

does not work, the value 4 is not found. If you change it to 0-59 like so, with the seconds it works perfectly.

Is this a bug, or what is going on here?

3
  • 1
    See Why doesn't [01-12] range work as expected? Commented Apr 25, 2017 at 13:47
  • The interesting question is: why did the other range ([0-59]) work? Turns out it does not. Try validating 17 with it… Commented Apr 25, 2017 at 13:51
  • The thing is: the whole character set only matches a single character in the set. You kind of recognized this when you added a + after the character set so it matches multiple times… Commented Apr 25, 2017 at 13:54

1 Answer 1

1

[0-23] doesnt mean from 0 to 23. It means from 0 to 2 and 3. So it matches 0,1,2 and 3. Nothing else.

It will be probably simpler to compare hourEntered >= 0 and hourEntered < 24

or use more complicated regex

Sign up to request clarification or add additional context in comments.

3 Comments

what aboute if [ $hourEntered -ge 0 ] && [ $hourEntered -le 23 ]; then
yep, that should work. did you try it?
Works as expected

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.