shell if 判断,字符正则匹配

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

格式

[[ 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}" 强行字符串匹配;

增强版正则匹配

这种更通用版本, 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")

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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: shell