Regular expressions in a Bash case statement
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*.host.com
(or ws-+([0-9]).host.com
, but that looks a bit advanced and I've never tried that :-)
If you want assert that *
really matches digits in ws*.host.com
and want to use case
instead of if
, elif
, elif
...you can use something like that:
case $SERVER in ws-[0123456789][0123456789][0123456789].host.com) echo "Web Server" ;; db-[0123456789][0123456789][0123456789].host.com) echo "DB server" ;; bk-[0123456789][0123456789][0123456789].host.com) echo "Backup server" ;; *) echo "Unknown server" ;;esac
But that does not work for more than 999 servers.
If I had to make a script for this use case, I probably write something like that (because I love regexes and case syntax ;) ):
srv=`expr "$SERVER" : '^\(db\|bk\|ws\)-[0-9]\+\.host\.com$'`echo -n "$SERVER : "case $srv in ws) echo "Web Server" ;; db) echo "DB server" ;; bk) echo "Backup server" ;; *) echo "Unknown server !!!"esac