分页: 1 / 1

哪个大牛帮看下脚本?

发表于 : 2012-01-12 9:59
firebooder
原文:
DISTRO=$(lsb_release -c -s)

if [[ ! ${DISTRO} =~ (oneiric) ]]; then
echo "WARNING: this script has only been tested on oneiric"
if [[ "$FORCE" != "yes" ]]; then
echo "If you wish to run this script anyway run with FORCE=yes"
exit 1
fi
fi
最近在看一款开源软件,这是其中的一个脚本,脚本这段处理的意思大概知道,但是问题是对if里面的这个判断很不解?
${DISTRO} =~ (oneiric)这样写没错误么?

Re: 哪个大牛帮看下脚本?

发表于 : 2012-01-12 10:24
tusooa
这是Bash匹配正则吧。

man bash =

代码: 全选

              An additional binary operator, =~, is available, with the same precedence as == and !=.  When it is used, the  string  to
              the  right  of  the  operator is considered an extended regular expression and matched accordingly (as in regex(3)).  The
              return value is 0 if the string matches the pattern, and 1 otherwise.  If the regular expression is syntactically  incor‐
              rect, the conditional expression's return value is 2.  If the shell option nocasematch is enabled, the match is performed
              without regard to the case of alphabetic characters.  Any part of the pattern may be quoted to force it to be matched  as
              a  string.  Substrings matched by parenthesized subexpressions within the regular expression are saved in the array vari‐
              able BASH_REMATCH.  The element of BASH_REMATCH with index 0 is the portion of the string  matching  the  entire  regular
              expression.   The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subex‐
              pression.
()是分组。那oneiric会放在${BASH_REMATCH[1]}里边

Re: 哪个大牛帮看下脚本?

发表于 : 2012-01-12 10:45
firebooder
谢谢哈,正则...遗漏了一大块没学习呐~

Re: 哪个大牛帮看下脚本?

发表于 : 2012-01-12 13:35
cuihao
正则应用很广,但格式又各有不同…… 反正用到时我就Google一下。

Re: 哪个大牛帮看下脚本?

发表于 : 2012-01-12 14:57
eexpress
bash从某版本开始,才支持这写法的。

正则,最好使用perl -e 来处理。那正宗的正则。