Linux笔记之bash条件测试

bash条件测试

bash条件测试

常用测试命令

常用的测试命令有以下三中方式,其中使用最多的是第二种。

test Expression

[ Expression ]

[[ Expression ]]
Note:Expression 指要测试的表达式,且其前后必须要有空格,否则视为语法错误;[][[]]有所不同, []是命令,[[]]是Linux中的关键字。

bash测试类型
主要涉及到三类测试类型,分别是数值测试,字符串测试,文件测试,可以使用man bash 查看所有的测试表达式。下面对三种测试类型常用的测试表达式进行阐述和演示。

数值比较

[ a -gt b ] : a是否大于b
[ a -ge b ] : a是否大于等b
[ a -eq b ] : a是否等于b
[ a -ne b ] : 是否不等于b
[ a -lt b ] : a是否小于b
[ a -le b ] : a是否小于等于b

在linux中可以使用·echo $? 查看上一条命令是否执行成功,其中0 表示成功, 1-255 表示失败,每一个数字表示不同的失败原因。 下面是对以上知识点的应用和验证,分别使用三种不同的测试命令。

2.字符串测试 (通过比较ASCALL码值进行比较)

[ “str1” ==“str2” ] : str1是否等于str2
[ “str1” > “str2” ] : str1是否大于str2
[ “str1” < “str2” ] : str1是否小于str2
[ “str1”!=“str2” ] : str1是否不等于str2
[ “str1”~= “str2” ] : str1是否被str2所匹配
[ -z “string”] : 测试字符串或变量是否为空,为空则为真,不空为假
[ -n“string”] : 测试变量是否不为空,不空则为真,空则为假


Note:用字符串比较时用到的操作数都应该使用引号,如下测试所示,不使用引号和使用引号完全是两种不同的含义。
3.文件测试
存在性测试:
-a file: True if file exists
-e file:True if file exists

存在性及类别判断: 
-c file: True if file exists and is a character special file
-b file: True if file exists and is a block special file
-d file: True if file exists and is a directory
-f file: True if file exists and is a regular file
-h or -L file: True if file exists and is a symbolic link
-p file: True if file exists and is a namedd pipe(FIFO)
-S file: True if file exists and is a Socket file

文件权限测试:
-r file: True if file exists and is readable
-w file: True if file exists and is writable
-x file: True if file exists and is execuatable

文件特殊权限测试:
-g file: True if file exists and is set-group-id.(SGID特殊权限位)
-u file: True if file exists and is set-user-id.(SUID特殊权限位)
-k file: True if file exists and its ``sticky'' bit is set.

文件大小测试
-s: True if file exists and its ``sticky'' bit is set.

文件是否打开
-t fd  True if file descriptor fd(文件描述符) is open and refers to a terminal.

-N file True if file exists and has been modified since it was last read.
-O file: 当前用户是否为文件属主
-G file: 当前用户是否为文件属组

双目测试:
 file1 -ef file2: True if file1 and file2 refer to the same device and inode numbers.(两个文件是否指向同一个设备上相同的inode)

 文件版本测试:
 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.

4、组合测试条件
Note:表达式需要结合测试命令
将多种条件测试做逻辑运算,共有两种方式:

第一种方式-命令式
 - COMMAND1 && COMMAND2
 - COMMAND1 || COMMAND2
 - ! COMMAND
 - Exam: [ -e FILE ]  && [ -r FILE ]
 第二种方式-表达式
-Expression1 -a Expression2
-Expression1 -o expressions
-! Expressions

Example:判断 hostname是否为空,或其值是否为localhost.localdomain
4、bash自定义退出码
command: exit num

Note:
1、脚本中一旦遇到exit命令,脚本会立即终止;终止退出状态取决于exit命令后面的数字。
2、如果未给脚本指定退出状态码,整个脚本的退出状态码取决于脚本中执行的最后一条命令的状态码

-e $file: 是否存在,存在则为真;
-a $file: 同上,弃用;
-f $file: 文件是否存在,且为普通文件;
-d $file: 是否存在,且为目录;
-h $file: 是否存在,且为符号链接文件;
-l $file: 同上;
-b $file: 是否存在,且为块设备文件;
-S $file: 是否存在,且为套接字文件;
-c $file: 是否存在,且为字符设备文件;
-p $file: 是否存在,且为管道文件;

-r $file: 当前用户是否对此文件拥有读权限;
-w $file: 当前用户是否对此文件拥有写权限;
-x $file: 当前用户是否对此文件拥有执行权限;
-u $file: 文件是否拥有suid权限;
-g $file: 文件是否拥有sgid权限;
-k $file: 文件是否拥有sticky权限;

-O $file: 当前用户是否为文件的属主;
-G $file: 当前用户是否属于文件的属组;

-N $file: 文件自从上一次被读取之后,是否被修改过;
$f1 -nt $f2:文件f1是否比文件f2新;
$f1 -ot $f2:文件f1是否比文件f2旧;
$f1 -ef $f2:文件f1和文件f2是否为同一个文件的硬链接;

单分支if语句
if CONDITION is True; then
    分支
fi
双分支if语句
if CONDITION is True; then
    分支
else
    分支
fi
多分支if语句
if CONDITION is True; then
    分支
elif CONDITION is True; then
    分支2
...
else
    分支n
fi

你可能感兴趣的:(Linux笔记,linux,centos,运维)