-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?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
MMM
  • 143
  • 1
  • 8
  • 1
    See [Why doesn't \[01-12\] range work as expected?](http://stackoverflow.com/questions/3148240/why-doesnt-01-12-range-work-as-expected) – Wiktor Stribiżew Apr 25 '17 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… – Raphael Schweikert Apr 25 '17 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… – Raphael Schweikert Apr 25 '17 at 13:54

1 Answers1

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

gaspo
  • 42
  • 4