bash之条件测试

bash之条件测试

  • 概念
    判断某需求是否满足,需要由测试机制来实现。比如说测试某一用户是否存在,可以直接使用id命令查看或者使用cat /etc/passwd命令进行查看,但要比较两个数值的大小,在命令行中输入2 > 3显然是不行的,shell会把第一个字串当做命令使用。所以专用的测试表达式需要由测试命令完成测试过程

  • 常用测试命令

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

  1. test Expression

  2. [ Expression ]

  3. [[ Expression ]]

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

  • bash测试类型

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

  1. 数值比较

    [ 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 表示失败,每一个数字表示不同的失败原因。 下面是对以上知识点的应用和验证,分别使用三种不同的测试命令。

[root@localhost ~]# test 1 -gt 2
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ 1 -eq 2 ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [[ 1 -ge 2 ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ 1 -ne 2 ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ 1 -lt 2 ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ 1 -le 2 ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [1 -le 2] 
bash: [1: command not found...
[root@localhost ~]# 

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”] : 测试变量是否不为空,不空则为真,空则为假

下面是对以上知识点的应用和验证,分别使用三种不同的测试命令。

[root@localhost ~]# name1="a"
[root@localhost ~]# name2="b"
[root@localhost ~]# name3="c"
[root@localhost ~]# name4=""
[root@localhost ~]# name5="a"
[root@localhost ~]# test $name1 != $name5
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ $name1 == $name5 ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [[ $name1 > $name2 ]]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -z "$name4" ]
[root@localhost ~]# echo $?
0

Note:用字符串比较时用到的操作数都应该使用引号,如下测试所示,不使用引号和使用引号完全是两种不同的含义。

[root@localhost ~]# test -n $name4
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ -n "$name4" ]
[root@localhost ~]# echo $?
1

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

方法1
[root@localhost ~]# [ -z "$hostName" ] || [ "$hostName"=="localhost.localdomain" ] 
[root@localhost ~]# echo $?
0
方法2
[root@localhost ~]# [ -z "$hostName" -o "$hostName"=="localhost.localdomain" ] 
[root@localhost ~]# echo $?
0

4、bash自定义退出码
command: exit num

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

Practice:写一个小脚本:
接受一个文件路径作为参数,如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;
如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数;
实现功能即可。

#! /usr/bin/bash

if [ $# -lt 1 ]
then
    echo "please input at least one argurments"
    exit 1
else
    spaceCount=`cat $1 | grep -E "^[[:space:]]*$" | wc -l`
    echo "$spaceCount"
    echo "there are $spaceCount is null in $1"
fi
[root@localhost ~]# ls
anaconda-ks.cfg  conutSpace.sh  initial-setup-ks.cfg  test
[root@localhost ~]# bash conutSpace.sh test
5
there are 5 is null in test
[root@localhost ~]# cat test
ias

asd

asd
ad
asd



adsdasadsd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值