24_Shell語言————if條件判斷之字符測試

前文中介紹過,bash的條件測試主要有如下3類:shell

整數測試:比較兩個整數誰大誰小,是否相等;vim

字符測試:比較兩個字符串是否相等;bash

文件測試:測試某個文件是否具備讀權限、寫權限、執行權限等;ssh

整數測試在前文中介紹過,這裏着重講解字符測試。字符測試採用的比較符號是經常使用的數學符號:ide

>:大於(在ASCII碼錶中的前後順序,從左至右逐字比較)測試

<:小於ui

==:等於(注意,= 表示賦值)spa

=~:判斷左邊的字符串是否可以被右邊的模式所匹配,一般用於雙中括號中:命令行

[[ $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
相關文章
相關標籤/搜索