前文中介绍过,bash的条件测试主要有以下3类:

整数测试:比较两个整数谁大谁小,是否相等;

字符测试:比较两个字符串是否相等;

文件测试:测试某个文件是否具有读权限、写权限、执行权限等;

整数测试在前文中介绍过,这里着重讲解字符测试。字符测试采用的比较符号是常用的数学符号:

>:大于(在ASCII码表中的先后顺序,从左至右逐字比较)

<:小于

==:等于(注意,= 表示赋值)

=~:判断左边的字符串是否能够被右边的模式所匹配,通常用于双中括号中:

[[ $opt1=~$opt2 ]]

通常做行首行尾锚定,不要加上引号。

 

上述比较都是两个变量的比较,但bash中也可以进行单目测试,即只测试一个变量:

-z$STRING: 为空则为真,不空则为假

-n$STRING: 为空则为假,不空则真

 

下面来举例说明字符测试的用法:

1:写一个脚本,判定用户的shell是否为bash

可以定义一个变量Shell来存放用户的shell,然后做判断:[ “$Shell” == “/bin/bash”],可以现在命令行中初步实验:

[root@localhosttutor]# Shell="/bin/tcsh"

#定义变量Shell,赋值为“/bin/tcsh

[root@localhosttutor]# [ $Shell == "/bin/bash" ]

#判断Shell是否为“/bin/bash

[root@localhosttutor]# echo $?

1

#查看测试结果

[root@localhosttutor]# Shell="/bin/bash"

[root@localhosttutor]# [ $Shell == "/bin/bash" ]

#注意,$Shell最好也加上引号,因为如果字符串中有空格,有可能会被当做多个变量

[root@localhosttutor]# echo $?

0

具体的脚本如下:

[root@localhosttutor]# vim if_shell.sh

#!/bin/bash
#
 
Shell=`grep "^$1:"/etc/passwd | cut -d: -f7`
 
if [ "$Shell" =="/bin/bash" ]; then
        echo "Bash User."
        Ret=0
else
        echo "Not Bash User."
        Ret=9
fi
 
exit $Ret

[root@localhosttutor]# bash -n if_shell.sh

[root@localhosttutor]# bash if_shell.sh root

Bash User.

[root@localhosttutor]# bash -x if_shell.sh daemon

++ cut -d: -f7
++ grep '^daemon:' /etc/passwd
+ Shell=/sbin/nologin
+ '[' /sbin/nologin == /bin/bash']'
+ echo 'Not Bash User.'
Not Bash User.
+ Ret=9
+ exit 9

[root@localhosttutor]# bash if_shell.sh roott

Not Bash User.

 

这里roott用户并不存在,如果在脚本中先对用户存在与否做判断,会使得脚本更加完善。可以使用-z来进行字符判断:

 

[root@localhosttutor]# echo $Shell

/bin/bash

[root@localhosttutor]# [ -z $Shell ]

#-z 来判断变量Shell中是否有值,有即为假,无即为真。

[root@localhosttutor]# echo $?

1

[root@localhosttutor]# unset Shell

#撤销变量Shell

[root@localhosttutor]# [ -z $Shell ]

[root@localhosttutor]# echo $?

0

故此脚本可以作如下改进

[root@localhosttutor]# vim if_shell.sh

#!/bin/bash
#
 
Shell=`grep "^$1:"/etc/passwd | cut -d: -f7`
 
if [ -z $Shell ]; then
#先判断变量Shell中是否有值,如果有,那么为假,语句不执行;如果没有值,则为真,执行下面的语句
        echo "No such user or User's shell is null."
        exit 10
#直接中断脚本的执行
fi
 
 
if [ "$Shell" =="/bin/bash" ]; then
        echo "Bash User."
        Ret=0
else
        echo "Not Bash User."
        Ret=9
fi
 
exit $Ret

 

[root@localhosttutor]# bash -x if_shell.sh roott

++ cut -d: -f7
++ grep '^roott:' /etc/passwd
+ Shell=
+ '[' -z ']'
+ echo 'No such user or User'\''sshell is null.'
No such user or User's shell isnull.
+ exit 10

 

还是用上面这个例子,但只需要判断用户的shell是否以sh结尾,就采用模式匹配的方式来进行字符串匹配:

 

[root@localhosttutor]# Shell=/bin/bash

[root@localhosttutor]# [[ “$Shell” =~ sh$ ]]

#采用行尾锚定的方式进行模式匹配,判断变量Shell是否以sh结尾

[root@localhosttutor]# echo $?

0

 

上述示例改进为判断一个用户的shell是否以sh结尾,是就显示为可登陆用户,否就显示为非登陆用户

 

[root@localhosttutor]# vim if_shell_sh.sh

#!/bin/bash
#
 
Shell=`grep "^$1:"/etc/passwd | cut -d: -f7`
 
if [ -z $Shell ]; then
        echo "No Shell."
        exit 3
fi
 
if [[ "$Shell" =~ sh$]]; then
        echo "Login User"
        Ret=0
else
        echo "None Login User."
        Re4=4
fi
 
exit $Ret

 

[root@localhosttutor]# bash -n if_shell_sh.sh

[root@localhosttutor]# bash if_shell_sh.sh root

Login User

[root@localhosttutor]# bash if_shell_sh.sh roott

No Shell.

[root@localhosttutor]# bash -x if_shell_sh.sh daemon

++ cut -d: -f7
++ grep '^daemon:' /etc/passwd
+ Shell=/sbin/nologin
+ '[' -z /sbin/nologin ']'
+ [[ /sbin/nologin =~ sh$ ]]
+ echo 'None Login User.'
None Login User.
+ Re4=4
+ exit

[root@localhosttutor]# useradd -s /bin/tcsh hello

#创建一个用户,指定其shelltcsh

[root@localhosttutor]# bash -x if_shell_sh.sh hello

++ cut -d: -f7
++ grep '^hello:' /etc/passwd
+ Shell=/bin/tcsh
+ '[' -z /bin/tcsh ']'
+ [[ /bin/tcsh =~ sh$ ]]
+ echo 'Login User'
Login User
+ Ret=0
+ exit 0

 

2. 写一个脚本:判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中。如果其生产商为GenuineIntel,就显示其为Intel公司;否则,就显示其为AMD公司

可以使用grep命令取出主机的生产商:

[root@localhosttutor]# cat /proc/cpuinfo | grep "vendor_id" | uniq

vendor_id       : GenuineIntel

[root@localhosttutor]# cat /proc/cpuinfo | grep "vendor_id" | uniq | cut -d: -f2

 GenuineIntel
#注意,做模式匹配时需要注意空格


 

此脚本可以写成:

[root@localhosttutor]# vim if_cup.sh

#!/bin/bash
#
Vendor=`grep"vendor_id" /proc/cpuinfo | uniq | cut -d: -f2`
 
if [[ "$Vendor" =~[[:space:]]*GenuineIntel$ ]]; then
        echo "Intel"
else
        echo "AMD"
fi

[root@localhosttutor]# bash -n if_cup.sh

[root@localhosttutor]# bash -x if_cup.sh

++ cut -d: -f2
++ uniq
++ grep vendor_id /proc/cpuinfo
+ Vendor=' GenuineIntel'
+ [[  GenuineIntel =~ [[:space:]]*GenuineIntel$ ]]
+ echo Intel
Intel

 

3. 写一个脚本,通过参数传递一个字符串给脚本,如果传递的字符串为“memory”或“Memory”,就以MB为单位显示当前主机的内存信息;否则,就显示/proc/uptime文件的内容

[root@localhosttutor]# vim memory.sh

#!/bin/bash
#
if [[ $1 =~ ^[Mm]emory$ ]]; then
        free -m
else
        cat /proc/uptime
fi

[root@localhosttutor]# bash -n memory.sh

[root@localhosttutor]# bash -x memory.sh memory

+ [[ memory =~ ^[Mm]emory$ ]]
+ free -m
             total       used       free    shared    buffers     cached
Mem:           996        509        487         0         54        160
-/+ buffers/cache:        293        702
Swap:         2015          0       2015

[root@localhosttutor]# bash -x memory.sh Memory

+ [[ Memory =~ ^[Mm]emory$ ]]
+ free -m
             total       used       free    shared    buffers     cached
Mem:           996        509        487        0         54        160
-/+ buffers/cache:        293        702
Swap:         2015         0       2015

[root@localhosttutor]# bash -x memory.sh abc

+ [[ abc =~ ^[Mm]emory$ ]]
+ cat /proc/uptime
47430.02 46732.82