I'm adding a feature to an existing script that will allow the user to configure the hostname of a Linux system. The rules I'm enforcing are as follows:
- Must be between 2 and 63 characters long
- Must not start or end with a hyphen
- Can only contain alphanumeric characters, and hyphens; all other characters are not allowed (including an underscore, which means I can't use the \W regex symbol)
I've solved the first two in the list, but I'm having trouble figuring out how to check if a bash string contains only letters, digits, and hyphens. I think I can do this with a regex, but I'm having trouble figuring out how (I've spent the past hour searching the web and reading man pages).
I'm open to using sed, grep, or any of the other standard tools, but not Perl or Python.