格式
[[ expression ]]
True: 0 ; False 1,2,3,...
处理条件表达式; expression
可以是一个, 也可以是多个, 通过逻辑运算符连接;
可以[[ expr ]] && bash_expression
, 也可以[[expr]];echo $?;
并不一定用在if while
等条件判断上, 也可以用在其他场景;
处理
-
不进行: 文件扩张, 分词; 其他的语法处理会执行;
-
注意条件操作关键字不要括起来;
-f
而不是"-f"
; -
> <
字符串比较和当前设置地区有关, 不同的地区字符排序不一样; -
== !=
的右边不是字符则被当成正则表达式; 可以用变量保存, 也可以通过引号强行将变量结果转为字符串; 推荐使用=~
增强版正则; -
shopt -s extglob
设置后,=
等价于==
; -
shopt -s nocasematch
设置后, 忽略大小写; -
=~
右边的字符被当成是拓展正则, 即更通用的正则; 可以生成组; 结果BASH_REMATCH
数组保存;0
是匹配部分, 后面则是组号;常用^$
; -
()
优先级;&& || !
与或非;
比较运算符
文件
-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}" 强行字符串匹配;
增强版正则匹配
这种更通用版本, python
的re
, 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进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。