在bash case语句经常EX pression [英] regular expression in bash case statement

查看:116
本文介绍了在bash case语句经常EX pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的脚本,它使用case语句找到服务器。

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

    #!/bin/bash
SERVER=$1;
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"
;;
esac

但它不工作。正则表达式正与egrep的,但不是这样。样品O / P

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

./test-back.sh ws-23.host.com
ws-23.host.com
Unknown server

任何想法?

推荐答案

Bash的情况下不使用常规的前pressions,但是的shell模式只匹配

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

因此​​,与其正则表达式 WS- [0-9] + \\主机\\ .COM 你应该使用模式 WS *。主持人。 COM (或 WS - +([0-9])host.com ,但看起来有点先进的,我从来没有尝试过。即: - )

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 :-)

这篇关于在bash case语句经常EX pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆