哪个大牛帮看下脚本?

sh/bash/dash/ksh/zsh等Shell脚本
回复
firebooder
帖子: 2
注册时间: 2011-10-08 13:19

哪个大牛帮看下脚本?

#1

帖子 firebooder » 2012-01-12 9:59

原文:
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)这样写没错误么?
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

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

#2

帖子 tusooa » 2012-01-12 10:24

这是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]}里边

代码: 全选

] ls -ld //
firebooder
帖子: 2
注册时间: 2011-10-08 13:19

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

#3

帖子 firebooder » 2012-01-12 10:45

谢谢哈,正则...遗漏了一大块没学习呐~
头像
cuihao
帖子: 4793
注册时间: 2008-07-24 11:33
来自: 郑州
联系:

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

#4

帖子 cuihao » 2012-01-12 13:35

正则应用很广,但格式又各有不同…… 反正用到时我就Google一下。
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

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

#5

帖子 eexpress » 2012-01-12 14:57

bash从某版本开始,才支持这写法的。

正则,最好使用perl -e 来处理。那正宗的正则。
● 鸣学
回复