6.3bash條件測試

    條件測試:
        整型測試:數值間的大小比較
            -gt, -lt, -eq, -ne, -ge, -le
        字符串測試:字符串大小比較
            >, <, ==, !=, =~, -n不空爲真, -z
        文件測試
       
[root@linux_basic ~]#type hostname
hostname is hashed (/bin/hostname)
[root@linux_basic ~]#hostname --help
Usage: hostname [-v] {hostname|-F file}      set hostname (from file)  跟參數,則爲設置主機名
       domainname [-v] {nisdomain|-F file}   set NIS domainname (from file)
       hostname [-v] [-d|-f|-s|-a|-i|-y|-A|-I]  display formatted name
       hostname [-v]                         display hostname  不接參數則是顯示主機名linux

       hostname -V|--version|-h|--help       print info and exitshell

    dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -ybash

    -s, --short           short host name
    -a, --alias           alias names
    -i, --ip-address      addresses for the hostname
    -I, --all-ip-addresses all addresses for the host
    -f, --fqdn, --long    long host name (FQDN)
    -A, --all-fqdns        all long host names (FQDNs)
    -d, --domain          DNS domain name
    -y, --yp, --nis       NIS/YP domainname
    -F, --file            read hostname or NIS domainname from given fileless

   This command can read or set the hostname or the NIS domainname. You can
   also read the DNS domain or the FQDN (fully qualified domain name).
   Unless you are using bind or NIS for host lookups you can change the
   FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
   part of the FQDN) in the /etc/hosts file.
  
    例如:若是當前主機的主機名爲localhost,則將其修改成www.magedu.comdom

        比較時,
        if [ `hostname` == 'localhost' ]; then
            hostname www.magedu.com
        fi
---------------------------------------------------------------
[root@linux_basic scripts]#cat hostname.sh
#!/bin/bash
#
if [ "$(hostname)" == "localhost" ]
then
    hostname www.magedu.com
fi
[root@linux_basic scripts]#bash -n hostname.sh
[root@linux_basic scripts]#chmod +x hostname.sh
[root@linux_basic scripts]#./hostname.sh
[root@linux_basic scripts]#hostname localhost
[root@linux_basic scripts]#hostname
localhost
[root@linux_basic scripts]#./hostname.sh
[root@linux_basic scripts]#hostname
www.magedu.com
---------------------------------------------------------------測試

[root@linux_basic ~]#echo $srui

[root@linux_basic ~]#[ $st == 'localhost' ]
-bash: [: ==: unary operator expected  要求的是雙目運算符的操做,而這裏是只有一個
操做數($sr是爲空值的)
要模擬空值比較,須要空值加雙引號才行
[root@linux_basic ~]#[ "$st" == 'localhost' ]
[root@linux_basic ~]#echo $?  以上狀態爲假,因此值不爲0
1this

    例如:若是當前主機的主機名爲空,則將其修改成用戶經過命令行參數傳遞過來的用戶名
        hostName=`hostname`命令行

        if [ -z "$hostName" ]; then
            hostname $1
        fi           這裏是須要注意 空格和值爲空 是不一樣的概念的
-----------------------------------------------------------------
[root@linux_basic scripts]#cat hostname2.sh
#!/bin/bash
#
if [ -z "$(hostname)" ];then
   hostname $1
fi
[root@linux_basic scripts]#bash -n hostname2.sh
[root@linux_basic scripts]#chmod +x hostname2.sh
[root@linux_basic scripts]#./hostname2.sh root
[root@linux_basic scripts]#hostname
www.magedu.com
[root@linux_basic scripts]#hostname ' '
[root@linux_basic scripts]#hostname
 
[root@linux_basic scripts]#./hostname2.sh root
[root@linux_basic scripts]#hostname
 
[root@linux_basic scripts]#echo "hostname >> hostname2.sh"
hostname >> hostname2.sh
[root@linux_basic scripts]#echo "hostname" >> hostname2.sh
[root@linux_basic scripts]#./hostname2.sh root
 
[root@linux_basic scripts]#cat hostname2.sh
#!/bin/bash
#
if [ -z "$(hostname)" ];then
   hostname $1
fi
hostname
[root@linux_basic scripts]#bash -x ./hostname2.sh root
++ hostname
+ '[' -z ' ' ']'
+ hostname
 
[root@linux_basic scripts]#hostname ''
[root@linux_basic scripts]#bash -x ./hostname2.sh root
++ hostname
+ '[' -z '' ']'
+ hostname root
+ hostname
root         由於hostname是環境變量,因此在當前shell和其子shell下,值都是修改事後的
--------------------------------------------------------------------        orm

    組合條件測試:在多個條件間實現邏輯運算
    經常使用的三種運算: 與運算   或運算    非運算
        與 有兩種寫法:[ condition1 -a condition2 ]
              condition1 && condition2
        或 有兩種寫法:[ condition1 -o condition2 ]
              condition1 || condition2
        非 有兩種寫法:[ -not condition ]    注意這種:-not
            ! condition

        例如:若是當前主機的主機名爲空,或者爲"localhost",則將其修改成www.magedu.com
            #!/bin/bash
            #
            hostName=`hostname`

            if [ -z "$hostName" -o "$hostName" == 'localhost' ]; then
                hostname www.magedu.com
            fi

-----------------------------------------------------------------------
[root@linux_basic scripts]#cat hostname3.sh
#!/bin/bash
#
if [ -z "$(hostname)" -o "$(hostname)" == "localhost" ];then
   hostname www.magedu.com
fi

hostname
[root@linux_basic scripts]#bash -n hostname3.sh
[root@linux_basic scripts]#chmod +x hostname3.sh
[root@linux_basic scripts]#hostname
root
[root@linux_basic scripts]#hostname ''
[root@linux_basic scripts]#hostname

[root@linux_basic scripts]#./hostname3.sh
www.magedu.com
[root@linux_basic scripts]#hostname
www.magedu.com
[root@linux_basic scripts]#hostname root
[root@linux_basic scripts]#./hostname3.sh
root
------------------------------------------------------------------------
        若是某用戶存在,則顯示id號:
            if id $userName &> /dev/null; then
                id -u $userName
            fi

        例如:輸入某用戶,若是用戶存在,且answer變量的值爲「yes",則顯示用戶的ID號;不然,說用戶選擇了退出;
            id $userName
            retVal=$?  保存執行狀態返回值

            if [ $retval -eq 0 -a "$answer" == 'yes' ]; then

            上述方式改成:

            if id $userName &> /dev/null && [ "$answer" =='yes' ]; then
id $userName &> /dev/null 這裏是命令的執行狀態
[ "$answer" =='yes' ]  這裏是字符串的比較,因此要有 [ ] 中括號
----------------------------------------------------------------------- 
[root@linux_basic scripts]#cat user1.sh
#!/bin/bash
#
answer="yes"
if id $1 &>/dev/null && [ "$answer" == 'yes' ]
then
    id -u $1
else
    echo "User select exit."
    exit 1
fi
[root@linux_basic scripts]#./user1.sh
0
[root@linux_basic scripts]#./user1.sh user
510
[root@linux_basic scripts]#id -u user
510
-------------------------------------------------------------------------

        例如:若是answer不爲"quit",也不爲"q",則說用戶選擇了繼續;此爲與

        例如:若是answer不爲quit或q,則說明用戶選擇了繼續;  此爲或

        德 摩根定律:
           
    練習:給定一個用戶,若是其shell爲/bin/bash且其ID號大於等於500,則說這是一個可登陸普通用戶;不然,則顯示其爲非登陸用戶或管理員。
[root@linux_basic scripts]#cat user.sh
#!/bin/bash
#
#if [ "$(grep "^${1}:" /etc/passwd|cut -d: -f7)" == "/bin/bash" ] && [ "$(id -u $1)" -ge 500 ]
下面寫法是錯誤的,由於 && 的使用不能在 [ ]中括號裏
#if [ "$(grep "^${1}:" /etc/passwd|cut -d: -f7)" == "/bin/bash" && "$(id -u $1)" -ge 500 ]
寫法正確
if [ "$(grep "^${1}:" /etc/passwd|cut -d: -f7)" == "/bin/bash" -a "$(id -u $1)" -ge 500 ]
then
     echo "The is can login user."
else
     echo "The is not login user."
fi


        #!/bin/bash
        #
        if ! id $1 &> /dev/null; then
            echo "No this user."
            exit 3
        fi

        userShell=$(grep "^$1\>" /etc/passwd | cut -d: -f7)
        userID=$(id -u $1)

        if [ "$userShell" == '/bin/bash' -a $userID -ge 500 ]; then
            echo "Login user."
        else
            echo "not login user."
        fi       
-----------------------------------------------------------
[root@linux_basic scripts]#cat islogin.sh
#!/bin/bash
#
if ! id $1 &>/dev/null;then
   echo "$1 no exists."
   exit 1   不存在時,記住退出狀態不能少了
fi

userid=$(grep "^$1:" /etc/passwd | cut -d: -f3)

if [ $userid -ge 500 ]
then
    echo "$1 is login."
else
    echo "$1 is no login."
fi
[root@linux_basic scripts]#bash -n islogin.sh
[root@linux_basic scripts]#./islogin.sh usr
usr no exists.
[root@linux_basic scripts]#./islogin.sh user
user is login.
[root@linux_basic scripts]#bash -x ./islogin.sh user100
+ id user100
+ echo 'user100 no exists.'
user100 no exists.
+ exit 1
--------------------------------------------------------------

    練習:寫一個腳本
        若是某用戶不存在,就添加之;
            #!/bin/bash
            #
            if ! id $1 &> /dev/null; then
                useradd $1
            fi       

    練習:寫一腳本
        一、添加10個用戶:tuser501-tuser510
            若是用戶不存在,才添加;若是存在,則顯示已經有此用戶
        二、顯示一共添加了多少個用戶;
            #!/bin/bash
            #
            declare -i count=0

            for i in {501..510}; do
                if id tuser$i &> /dev/null; then
                    echo "tuser$i exists."
                else
                    useradd tuser$i
                    let count++
                fi
            done

            echo "Total add $count users."


    練習:寫一腳本
        一、添加10個用戶:tuser601-tuser610
            若是用戶不存在,才添加,並以綠色顯示添加成功;若是存在,則以紅色顯示已經有此用戶;
        二、顯示一共添加了多少個用戶;
            #!/bin/bash
            #
            declare -i count=0

            for i in {501..510}; do
                if id tuser$i &> /dev/null; then
                    echo -e "\033[31mtuser$i\033[0m exists."
                else
                    useradd tuser$i
                    echo -e "add user \033[32mtuser$i\033[0m successfully."
                    let count++
                fi
            done

            echo "Total add $count users."       

    練習:寫一個腳本
        傳遞用戶名給腳本
        一、判斷此用戶的shell是否爲/bin/bash,若是是,則顯示此用戶爲basher
        二、不然,則顯示此用戶爲非basher
   
            #!/bin/bash
            #
            userShell=`grep "^$1\>" /etc/passwd | cut -d: -f7`
   
            if [ "$userShell" == '/bin/bash' ]; then
                echo "basher"
            else
                echo "not basher"
            fi       

切記命令引用須要使用 反引號``  或者是  $()  來取命令的值

相關文章
相關標籤/搜索