Regex – Regular expressions in a Bash case statement


I am using following script, which uses case statement to find the server.

echo $SERVER | egrep "ws-[0-9]+\.host\.com";
case $SERVER in
ws-[0-9]+\.host\.com) echo "Web Server"
db-[0-9]+\.host\.com) echo "DB server"
bk-[0-9]+\.host\.com) echo "Backup server"
*)echo "Unknown server"

But it is not working. Regex is working with egrep but not with case. sample O/P

Unknown server

Any Idea ?

Best Solution

Bash case does not use regular expressions, but shell pattern matching only.

Therefore, instead of regex ws-[0-9]+\.host\.com you should use pattern ws* (or ws-+([0-9]), but that looks a bit advanced and I've never tried that :-)