I'm trying to verify if a subdomain entered by a user is valid, but whatever I pass in, it's never valid. I know the regex is ok, so the problem is my "if" logic, however I'm new to shell/bash
#!/bin/bash
#
echo Enter the subdomain\'s name to configure.
read SUBDOMAIN
if [[ ! $SUBDOMAIN =~ [A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])? ]]; then
echo "$SUBDOMAIN is not a valid domain"
fi
Examples:
Would be accepted (regular subdomain names): test
Would not be accepted (invalid subdomain name): -
Would not be accepted (invalid subdomain name): (Empty)
Would not be accepted (invalid subdomain name): #$??&@#&?$##$
I would prefer using shell, but the parentheses in the regex make the script throw an error.
I'm not sure if it can be done with grep, but I never understood how to use grep and it always confused me.