每个人,我都对shell 脚本中的这个表达式有疑问:
expr "$VERSION" : "_@[^@]*@"
谁能告诉我这里的“@”代表什么?
这只是一个字面意思@。正@则表达式中没有特殊含义,尽管它可能在$VERSION.
匹配“下划线后跟@,后跟零个或多个非@ 字符,后跟@”。
来自man expr:
expr1 : expr2
The ``:'' operator matches expr1 against expr2, which must be a
regular expression. The regular expression is anchored to the
beginning of the string with an implicit ``^''. expr expects
"basic" regular expressions, see re_format(7) for more informa-
tion on regular expressions.
@is just @,因为它在正则表达式中没有特殊含义。因此,
expr _@foo@ : "_@[^@]*@"
将成功,并输出6(它是匹配字符的数量);尽管
expr _x@foo@ : "_@[^@]*@"
将在 中输出0并返回失败代码$?,因为它无法匹配任何内容。
如果您不熟悉正则表达式,则示例中给出的意思是:下划线 ( _) 后跟两个 at 符号 ( @),中间夹着任意数量的非 at 符号字符。