格式

[[ expression ]]

True: 0 ; False 1,2,3,...

处理条件表达式; expression可以一个, 也可以多个, 通过逻辑运算连接;

可以[[ expr ]] && bash_expression, 也可以[[expr]];echo $?; 并不一定用在if while条件判断上, 也可以用在其他场景;


处理


比较运算符

文件

-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and its set-group-id bit is set.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its "sticky" bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd
True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-N file
True if file exists and has been modified since it was last read.
-O file
True if file exists and is owned by the effective user id.
-S file
True if file exists and is a socket.

文件之间比较

file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.

file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.

file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.

执行环境判断

-o optname
True if the shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin (see Section 4.3.1 [The Set Builtin], page 62).

变量判断

-v varname
True if the shell variable varname is set (has been assigned a value).

-R varname
True if the shell variable varname is set and is a name reference.

字符串

-z string True if the length of string is zero.
-n string
string
True if the length of string is non-zero.

string1 == string2
string1 = string2
True if the strings are equal. When used with the [[ command, this performs pattern matching as described above (see Section 3.2.5.2 [Conditional Constructs],  page 11). ‘=should be used with the test command for posix conformance.

string1 != string2
True if the strings are not equal.

string1 < string2
True if string1 sorts before string2 lexicographically.
string1 > string2
True if string1 sorts after string2 lexicographically.

数值比较

arg1 OP arg2
OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. 
These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or
equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers. When used with the [[ command,
Arg1 and Arg2 are evaluated as arithmetic expressions (see Section 6.5 [Shell Arithmetic], page 94).

案例

不进行if之类的条件判断; 自行尝试;

执行

#!/bin/bash
[[ 1 -eq 1 ]]
echo $?
[[ 1 -eq 0 ]]
echo $?

" 输出
0
1
"

字符匹配

#!/bin/bash
[[ "good" == g* ]]
echo $?
[[ "good" == *d ]]
echo $?
[[ "good" == *c ]]
echo $?


"输出
0
0
1
"

变量保存正则

#!/bin/bash
PATTERN="g*"
[[ "good" == ${PATTERN} ]]
echo $?
PATTERN="*d"
[[ "good" == ${PATTERN} ]]
echo $?
PATTERN="*c"
[[ "good" == ${PATTERN} ]]
echo $?

"输出
0
0
1
"
# 可以使用  "${PATTERN}" 强行字符串匹配;

增强版正则匹配

这种更通用版本, pythonre, notepad++之类的正则表达式;

#!/bin/bash
INFO="19912344321:23:19990909"
INFO_PAT="([0-9]+):([0-9]+):([0-9]+)"
[[ $INFO =~ $INFO_PAT ]]
for i in "${BASH_REMATCH[@]}";do echo $i;done;
set | grep BASH_REMATCH

输出
19912344321:23:19990909
19912344321
23
19990909
BASH_REMATCH=([0]="19912344321:23:19990909" [1]="19912344321" [2]="23" [3]="19990909")

可以看到通过()形成组, 输出这些值和信息;

原文地址:https://blog.csdn.net/rubikchen/article/details/128720941

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_45614.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注