linux系统下,shell语法问题!

来源:百度知道 编辑:UC知道 时间:2024/05/31 13:19:50
我在一个脚本中,发现如下连接符号:"=~",比如:$TEST_A =~ $TEST_B.
TEST_B是一系列的变量列表,TEST_A是一个字符串,上面表达式是否是在TEST_B中寻找TEST_A?
有可能是寻找变量的意思,但不能完全确定。有没有类似的语法介绍?我在shell语法中好像没有类似的介绍?

=~ 是bash3.0 以后版本的操作符,用来比对正规表达式
perl 也是相同, 你的可能是perl, bash 会用
[[ "$TEST_A" =~ $TEST_B ]]
这样的语法, perl 常见的会这样

$TEST_A =~ m/$TEST_B/;

RTFM:
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 expres-
sion 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 incorrect, the conditional
expression's return value is 2.

...

推荐用法
var=pattern
[[ $string =~ $var ]]

我搜藏中的论坛中,有篇帖子专门讲shell编程的,可惜baidu不让我贴地址啊,郁闷ing

~ 表示当前用户home目录