0

Any idea what I did wrong in this conditional regex?

MIN="30"

if ! [[ "$MIN" =~ [0-5]?[0-9]|\* ]]; then
  echo "INVALID MINUTE"
else
  echo "VALID MINUTE"
fi

Thanks

* UPDATE *

You didn't include the wildcard match. I need to do this using pure regex as I have other conditions that need to be met as well such as hour, month (more complex), and day of week like so:

[1-2]?[0-9]|\* - This is for hour
[1-3]?[0-9]|\* - This is for day of month
1?[0-9]|\*     - This is for month of year 
[0-7]|\*       - This is for day of week (0 = sunday etc)

ie minutes must be number for first and second place holders or can be a wildcard to match every possible minute 0 - 59

In fact now that I look at it, this solution will not work for day of month as someone could enter 39 which is not a valid day of the month.

* UPDATE *

Well I didn't completely follow your logic at first, but I think you're on to something. This may actually work and will be simpler. I like over complicating things...

if ! [[ "$MIN" -gt 0 && "$MIN" -lt 59 || "$MIN" == "\*" ]]; then

I'm just having trouble now with it literally evaluating (well comparing) the wildcard.

Any thoughts here. Googling in the mean time.

* UPDATE *

if [ "$MIN" -gt 0 ] && [ "$MIN" -lt 59 ] || [ "$MIN" = "*" ]; then

Just tested it again and checked my syntax. When MIN is between 0 and 59 it works great (true), when MIN is over 59 it also works (reports false), however as soon as you try to set MIN to an * the IF statement freaks and pops out:

line 340: [: *: integer expression expected
3
  • Comment on Update: you should not compare with wildcard (if you invent a way for this), as it will be always true. Use @Mehul Rathod answer instead. Commented Mar 6, 2016 at 12:56
  • Don't remember how I fixed this as it was awhile ago, but I was not intending to compare against a wildcard as an operator, but rather as just a character in a config file that represented a wildcard. You're probably right though. I had not considered it being interpreted that way. Doh! I was simply searching for a single character string containing a asterisk (If that helps to clear things up.) Commented Mar 7, 2016 at 17:31
  • Sorry, I didn't understand this! It were too big for comment, and I added the answer. Commented Mar 7, 2016 at 17:50

2 Answers 2

1

I think you are double negating your condition, also have you considered just using numeric operators?

min="30"

if ! [[ "$min" -gt 0 && "$min" -lt 59 ]]; then
  echo "INVALID MINUTE"
else
  echo "VALID MINUTE"
fi
Sign up to request clarification or add additional context in comments.

2 Comments

also use lower case variables in your script to avoid conflicting with system variables
Oh wow that ship has sailed lol. 2000 lines of code later, and I have been using all caps for variables since day one lol.
0

Your problem was (as explained in UPD) you had not been able to compare variable both with string and with integer in one conditional string.

I suggest you to compare vs string (* symbol) at first, then eval and compare to integer

MIN="*"; #try also other MIN values

k=0; # flag. 1 - if valid, 0 - if invalid

if [ "$MIN" == "*" ]; then
    k=1;
else
    eval "MIN=$MIN";
    if [ "$MIN" -gt 0 ] && [ "$MIN" -lt 59 ]; then
        k=1;
    fi;
fi;

if [ $k -eq 0 ]
    echo "INVALID MINUTE"
else
    echo "VALID MINUTE"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.