亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

《鳥哥的Linux私房菜》學習筆記(9)——條件判

系統 1811 0

一、條件判斷表達式 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

條件測試類型:

  • 整數測試
  • 字符測試
  • 文件測試

條件測試的表達式

  [ expression ](注意expression頭和尾部各有一個空格)

  [[ expression ]]

  test expression

算術運算有三種方法:

  • let 算術運算表達式
  • $[算術運算表達式]
  • $((算術運算表達式))
  • expr 算術運算表達式
      
        
          [root@hao ~]#
        
      
       A=
      
        3
      
      
        
           [root@hao ~]#
        
      
      B=
      
        4
      
      
        
           [root@hao ~]#
        
      
       let C=$A+
      
        $B


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        echo
      
      
         $C


      
      
        7
      
      
        
           [root@hao ~]#
        
      
       D=$[$A+
      
        $B]


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        echo
      
      
         $D


      
      
        7
      
      
        
           [root@hao ~]#
        
      
      E=$(($A+
      
        $B))


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        echo
      
      
         $E


      
      
        7
      
      
        
           [root@hao ~]#
        
      
       F=`
      
        expr
      
       $A +
      
         $B`


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        echo
      
      
         $F


      
      
        7
      
    

1、整數測試:

  -eq:測試兩個整數是否相等 $A -eq $ eB。

      
        
          [root@hao ~]#
        
      
      A=
      
        3
      
      
        
           [root@hao ~]#
        
      
       B=
      
        6
      
      
        
           [root@hao ~]#
        
      
      [ $A -
      
        eq $B ]

[root@hao 
      
      ~]# 
      
        echo
      
       $?


      
        1
      
    

  -ne:測試兩個整數是否不等

  -gt:測試一個數是否大于另一個數

  -lt:小于

  -ge:大于等于

  -le:小于或等于

語句間的邏輯關系(短路操作)

  邏輯與、邏輯或

下面代碼實現邏輯與,如果用戶存在,則第一個語句為TRUE,則執行第二個語句。否則,若用戶不存在,即第一個語句為false,則該邏輯與結果必為false,因此第二個語句就不會執行

      
        
          [root@hao ~]#
        
      
      
        id
      
       user1 &> /dev/
      
        null
      
       && 
      
        echo
      
      
        "
      
      
        hello,test true
      
      
        "
      
      
        
           [root@hao ~]#
        
      
      
        id
      
       hao &> /dev/
      
        null
      
       && 
      
        echo
      
      
        "
      
      
        hello,test true
      
      
        "
      
      
        

hello,test 
      
      
        true
      
    

(1)如果用戶不存在,則添加用戶(用邏輯與、邏輯或兩種方法實現)

      
        
          [root@hao ~]#
        
      
      
        id
      
      
         user2


      
      
        id
      
      
        : user2: No such user


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        id
      
       user2 &> /dev/
      
        null
      
       ||
      
         useradd user2


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        id
      
      
         user2

uid
      
      =
      
        5002
      
      (user2) gid=
      
        5002
      
      (user2) 
      
        groups
      
      =
      
        5002
      
      
        (user2)


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        id
      
      
         user3


      
      
        id
      
      
        : user3: No such user


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      ! 
      
        id
      
       user3 &> /dev/
      
        null
      
       &&
      
         useradd user3


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        id
      
      
         user3

uid
      
      =
      
        5003
      
      (user3) gid=
      
        5003
      
      (user3) 
      
        groups
      
      =
      
        5003
      
      (user3)
    

?(2) 如果一個文件的行數大于100,則顯示該文件為大文件,否則顯示為小文件

      #!/bin/
      
        bash

LINES
      
      =$(
      
        wc
      
       -l /etc/
      
        inittab)


      
      
        echo
      
      
         $LINES

FINLINES
      
      =`
      
        echo
      
       $LINES |
      
        cut
      
       -d
      
        '
      
      
        '
      
       -
      
        f1`


      
      
        echo
      
      
         $FINLINES

[ $FINLINES 
      
      -gt 
      
        100
      
       ]&& 
      
        echo
      
      
        "
      
      
        etc/inittab is a big file
      
      
        "
      
       || 
      
        echo
      
      
        "
      
      
        etc/inittab is a small file
      
      
        "
      
    

執行結果為:

      
        26
      
       /etc/
      
        inittab


      
      
        26
      
      
        

etc
      
      /inittab is a small 
      
        file
      
    

(3)如果用戶存在,則顯示該用戶,如果用戶不存在,則添加此用戶

      
        
          [root@hao ~]#
        
      
      
        id
      
       user1 && 
      
        echo
      
      
        "
      
      
        user1 exits.
      
      
        "
      
       ||
      
         useradd user1

uid
      
      =
      
        5001
      
      (user1) gid=
      
        5001
      
      (user1) 
      
        groups
      
      =
      
        5001
      
      
        (user1)

user1 exits.
      
    

(4)如果用戶不存在,則添加用戶并添加密碼,如果存在,則顯示用戶已存在

      
        
          [root@hao ~]#
        
      
      ! 
      
        id
      
       user1 && useradd user1 && 
      
        echo
      
      
        "
      
      
        user1
      
      
        "
      
       | 
      
        passwd
      
       --stdin user1 || 
      
        echo
      
      
        "
      
      
        user1 exits
      
      
        "
      
      
        

uid
      
      =
      
        5001
      
      (user1) gid=
      
        5001
      
      (user1) 
      
        groups
      
      =
      
        5001
      
      
        (user1)

user1 exits
      
    

(5)給定一個用戶,如果其UID為0,則顯示為管理員,否則顯示為普通用戶

      
        
          [root@hao ~]#
        
      
       nano third.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      
        cat
      
       third.
      
        sh
      
      
        

#
      
      !/bin/
      
        bash

NAME
      
      =
      
        user1

USERID
      
      =`
      
        id
      
       -
      
        u $NAME`

[ $USERID 
      
      -eq 
      
        0
      
       ] && 
      
        echo
      
      
        "
      
      
        Admin
      
      
        "
      
       || 
      
        echo
      
      
        "
      
      
        Common user
      
      
        "
      
      
        .


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
      
        chmod
      
       +x third.
      
        sh
      
      
        
           [root@hao ~]#
        
      
       ./third.
      
        sh
      
      
        

Common user.
      
    

? 2、文件測試

  -e file :測試文件是否存在

  -f file :測試文件是否為普通文件

  -d file:測試指定路徑是否為目錄

  -r file:測試指定文件對當前用戶是否可讀

  -w file: 測試指定文件對當前用戶是否可寫

  -x file: 測試指定文件對當前用戶是否可執行

(1)判斷一個文件是否存在

      #!bin /
      
        bash

FILE
      
      =/etc/
      
        inittab


      
      
        if
      
       [ ! -
      
        e $FILE ]


      
      
        then
      
      
        echo
      
      
        "
      
      
        no file
      
      
        "
      
      
        

    exit 
      
      
        8
      
      
        fi
      
    

(2)給定一個文件,如果不存在,則退出腳本,如果存在且該文件為普通文件或者目錄就顯示之,否則,

      #!/bin/
      
        bash

FILE
      
      =/etc/rc.d/
      
        rc.sysinit




      
      
        if
      
       [ ! -
      
        e $FILE ]


      
      
        then
      
      
        echo
      
      
        "
      
      
        no file
      
      
        "
      
      
        

    exit 
      
      
        6
      
      
        fi
      
      
        if
      
       [ -
      
        f $FILE  ]


      
      
        then
      
      
        echo
      
      
        "
      
      
        common file
      
      
        "
      
      
        elif
      
       [ -
      
        d $FILE ]


      
      
        then
      
      
        echo
      
      
        "
      
      
        directory
      
      
        "
      
      
        else
      
      
        echo
      
      
        "
      
      
        unknown
      
      
        "
      
      
        fi
      
    

二、條件判斷控制結構 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

1、單分支if語句:

if 判斷條件

then

  statement1

  statement2

  ...

fi

2、雙分支的if語句

if 判斷語句

then

  statement1

  statement2

else

  statement3

  statement4

fi

3、多分支的if語句

if 判斷條件1

then

  statement1

  ...

elif 判斷條件2

then

  statement2

  ...

elif 判斷條件3

then

  statement3

  ...

else

  statement4

  ...

fi

4、測試

(1)如果用戶存在,輸出用戶存在,否則創建用戶名和密碼為用戶名的密碼。

      
        
          [root@hao ~]#
        
      
       nano fourth.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      
        cat
      
       fourth.
      
        sh
      
      
         

#
      
      !/bin/
      
        bash

NAME
      
      =
      
        user4


      
      
        if
      
      
        id
      
       $NAME &> /dev/
      
        null
      
      
        then
      
      
        echo
      
      
        "
      
      
        $NAME exists
      
      
        "
      
      
        else
      
      
        

    useradd $NAME

    
      
      
        echo
      
       $NAME | 
      
        passwd
      
       --stdin $NAME &> /dev/
      
        null
      
      
        echo
      
      
        "
      
      
        add user successful
      
      
        "
      
      
        fi
      
      
        
           [root@hao ~]#
        
      
      
        chmod
      
       +x fourth.
      
        sh
      
      
        
           [root@hao ~]#
        
      
       ./fourth.
      
        sh
      
      
         

add user successful


        
          
            [root@hao 
          
        
      
      
        
          ~]#
        
      
       ./fourth.
      
        sh
      
      
         

user4 exists
      
    

(2)判斷一個用戶是否是管理員

      
        
          [root@hao ~]#
        
      
      nano fifth.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      
        cat
      
       fifth.
      
        sh
      
      
         

#
      
      !/bin/
      
        bash

NAME
      
      =
      
        user1


      
      
        if
      
       [ `
      
        id
      
       -u $NAME ` -eq 
      
        0
      
      
         ]


      
      
        then
      
      
        echo
      
      
        "
      
      
        Admin
      
      
        "
      
      
        else
      
      
        echo
      
      
        "
      
      
        Common
      
      
        "
      
      
        fi
      
      
        
           [root@hao ~]#
        
      
      
        chmod
      
       +x fifth.
      
        sh
      
      
        
           [root@hao ~]#
        
      
       ./fifth.
      
        sh
      
      
         

Common
      
    

(3)判斷當前系統上是否有用戶的默認shell為bash,如果有,就顯示有多少個這類用戶,否則,就顯示沒有這類用戶

      
        
          [root@hao ~]#
        
      
       nano sixth.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      
        cat
      
       sixth.
      
        sh
      
      
         

#
      
      !/bin/
      
        bash


      
      
        grep
      
      
        "
      
      
        \<bash$
      
      
        "
      
       /etc/
      
        passwd
      
       &> /dev/
      
        null
      
      
        

RETVAL
      
      =$?


      
        if
      
       [ $RETVAL -eq 
      
        0
      
      
         ]


      
      
        then
      
      
         

    USERS
      
      =`
      
        grep
      
      
        "
      
      
        \<bash$
      
      
        "
      
       /etc/
      
        passwd
      
       | 
      
        wc
      
       -
      
        l`

    
      
      
        echo
      
      
        "
      
      
        $USERS users has shell of bash
      
      
        "
      
      
        else
      
      
        echo
      
      
        "
      
      
        no such users
      
      
        "
      
      
        fi
      
      
        
           [root@hao ~]#
        
      
      
        chmod
      
       +x sixth.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      ./sixth.
      
        sh
      
      
        12
      
       users has shell of bash
    

(4)上題基礎上,如果存在上述用戶,顯示其中一個的用戶名,否則顯示沒有此類用戶

      
        
          [root@hao ~]#
        
      
      nano seventh.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      
        cat
      
       seventh.
      
        sh
      
      
         

#
      
      !/bin/
      
        bash


      
      
        grep
      
      
        "
      
      
        \<bash$
      
      
        "
      
       /etc/
      
        passwd
      
       &> /dev/
      
        null
      
      
        

RETVAL
      
      =$?


      
        if
      
       [ $RETVAL -eq 
      
        0
      
      
         ]


      
      
        then
      
      
         

    AUSER
      
      =`
      
        grep
      
      
        "
      
      
        \<bash$
      
      
        "
      
       /etc/
      
        passwd
      
       | 
      
        head
      
       -
      
        1
      
       | 
      
        cut
      
       -d: -
      
        f1`

    USERS
      
      =`
      
        grep
      
      
        "
      
      
        \<bash$
      
      
        "
      
       /etc/
      
        passwd
      
       | 
      
        wc
      
       -
      
        l`

    
      
      
        echo
      
      
        "
      
      
        One of the $USERS is $AUSER
      
      
        "
      
      
        else
      
      
        echo
      
      
        "
      
      
        no such users
      
      
        "
      
      
        fi
      
      
        
           [root@hao ~]#
        
      
      
        chmod
      
       +x seventh.
      
        sh
      
      
        
           [root@hao ~]#
        
      
      ./seventh.
      
        sh
      
      
         

One of the 
      
      
        12
      
       is root
    

(5)給定一個用戶,判斷其UID與GID是否一樣,如果一樣,就顯示此用戶為“good guy”;否則,就顯示此用戶為“bad guy”

      #!/bin/
      
        bash

USERNAME
      
      =
      
        user1

USERID
      
      =`
      
        id
      
       -
      
        u $USERNAME`

GROUPID
      
      =`
      
        id
      
       -
      
        g $USERNAME `


      
      
        if
      
       [ $USERID-
      
        eq $GROUPID]


      
      
        then
      
      
        echo
      
      
        "
      
      
        good guy
      
      
        "
      
      
        else
      
      
        echo
      
      
        "
      
      
        bad guy
      
      
        "
      
      
        fi
      
    

?三、測試腳本 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

測試腳本是否有語法錯誤

      bash -n 
      
        file.sh
      
    

單步執行腳本

      bash -x 
      
        file
      
      .
      
        sh
      
    

四、定義退出狀態碼 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

exit:退出腳本

exit #

如果腳本沒有明確定義退出狀態嗎,那么最后執行的一條命令的退出碼即為腳本的退出碼

?

?

?

?

?

?

?

?

?

?

?

?

《鳥哥的Linux私房菜》學習筆記(9)——條件判斷


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色婷婷久久久swag精品 | 久久精品大片 | 国产桃花视频 | 91大学生视频 | 青青国产成人久久91网 | 国产91在线 | 中文 | 亚洲精品成人一区 | 亚洲 欧美 精品 | 这里只有久久精品视频 | 欧美啪啪网站 | 高清不卡一区二区 | 亚洲精品成人久久 | 国产亚洲久久 | 欧美一区二区三区婷婷月色 | 久久久久久久亚洲精品一区 | 精品国产乱码一区二区三区麻豆 | 国产网红在线视频 | 精品动漫中文字幕一区二区三区 | 五月天激情亚洲婷婷在线 | 久久久久久免费视频 | 最新国产福利在线 | 久草在线中文 | 精品亚洲一区二区三区 | 97久久综合精品久久久综合 | 热久久久久久久 | 亚洲悠悠色综合中文字幕 | 欧美精品久久久久久久小说 | 欧美不卡视频在线 | 日一级片| 97影院九七理论片男女高清 | 精品国产一区二区三区2021 | 被黑人做的白浆直流在线播放 | 亚洲国产国产综合一区首页 | 91视频免费看 | 免费精品视频 | 香蕉视频日本 | 1024国产欧美日韩精品 | 九九香蕉网 | 亚洲乱码视频在线观看 | 免费一级毛片在级播放 | 一级欧美视频 |