For a /bin/sh
solution:
hostname=master01.nassa.com
case $hostname in
*[A-Z]*)
echo 'Not ok' ;;
*)
echo 'Ok'
esac
The "Not ok" branch is taken if the value of $hostname
contains any uppercase characters (this may depend on your locale settings), while the "Ok" branch is taken if the "Not ok" branch isn't taken.
This is similar to the bash
-specific (and also ksh
and zsh
)
if [[ "$hostname" == *[A-Z]* ]]; then
echo 'Not ok'
else
echo 'Ok'
fi
Your test with the regular expression ^[a-z.a-z.a-z]+$
is identical to a test with ^[a-z.]+$
(since a [...]
character class matches a single character and repeated a-z
inside it won't make a difference). This will actually do what you say it does, which is to allow only hostnames with lowercase letters (and dots). The issue is that your hostname also contains digits. To allow for these too, you would have to use ^[a-z0-9.]+$
. In this case though, it may be easier to test for the smaller set of characters that are not allowed, which is what I do above.