当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 5 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 哪个大牛帮看下脚本?
帖子发表于 : 2012-01-12 9:59 

注册: 2011-10-08 13:19
帖子: 2
送出感谢: 0 次
接收感谢: 0 次
原文:
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)这样写没错误么?


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 哪个大牛帮看下脚本?
帖子发表于 : 2012-01-12 10:24 

注册: 2008-10-31 22:12
帖子: 6546
系统: 践兔
送出感谢: 18
接收感谢: 25
这是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 //


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 哪个大牛帮看下脚本?
帖子发表于 : 2012-01-12 10:45 

注册: 2011-10-08 13:19
帖子: 2
送出感谢: 0 次
接收感谢: 0 次
谢谢哈,正则...遗漏了一大块没学习呐~


页首
 用户资料  
 
4 楼 
 文章标题 : Re: 哪个大牛帮看下脚本?
帖子发表于 : 2012-01-12 13:35 
头像

注册: 2008-07-24 11:33
帖子: 4793
地址: 郑州
送出感谢: 4
接收感谢: 92
正则应用很广,但格式又各有不同…… 反正用到时我就Google一下。


_________________
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~


页首
 用户资料  
 
5 楼 
 文章标题 : Re: 哪个大牛帮看下脚本?
帖子发表于 : 2012-01-12 14:57 
头像

注册: 2005-08-14 21:55
帖子: 58428
地址: 长沙
送出感谢: 4
接收感谢: 274
bash从某版本开始,才支持这写法的。

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


_________________
● 鸣学


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 5 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译