shell腳本編程之if、case條件語句

程序執行三種順序

    順序執行

    22.png

    選擇執行

    shell腳本編程之if、case條件語句

    循環執行

    shell腳本編程之if、case條件語句

    shell腳本編程之if、case條件語句

選擇執行語句:

    if語句 

    格式:

    單分支:    

    if 判斷條件;then

      條件分支代碼

    fi

    

     雙分支:

    if 判斷條件;then

     條件為真時語句

    else (此處不會對給出的條件做判斷)

     條件為假時語句

    fi

  

     多分支:

    if 條件1;then

     代碼

    elif 條件2;then

     代碼

     elid 條件2;then

     代碼

    else

      代碼

    fi

    例子:輸入兩個數,判斷其大小

        [root@centos7 bin]# cat f1
        #!/bin/bash
        read -p "input a number:" m
        read -p "input a number:" n
        if [ $m -gt $n ];then 
	    echo "$m >$n"
        else [ $m = $n ]                此程序中,else后面的判斷無效,如果m=n或者m<n都會走else分支,若想只要條件符合的輸出需要將else換成elif
	    echo " $m = $n"
        fi
        [root@centos7 bin]# 
        [root@centos7 bin]#

if語句例子

   if ping – c1 – W2 station1 &> /dev/null; then
    echo 'Station1 is UP'
   elif grep "station1" ~/maintenance.txt &> /dev/null
   then
    echo 'Station1 is undergoing maintenance‘
   else
    echo 'Station1 is unexpectedly DOWN!'
    exit 1
   fi
 

條件判斷case

  case 變量 in

   pat1)

    分支1

    ;;

   part2)

    分支2

    ;;

   part3)

    分支3

    ;;

   esac

 注意:case 支持glob的通配符

      
    循環語句綜合練習:
        1、寫一個腳本/root/bin/createuser.sh,實現如下功能:使用一個用戶名做為參數,如果指定參數的用戶存在,就顯
        示其存在,否則添加之;顯示添加的用戶的id號等信息
        2、寫一個腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,并判斷用戶輸入的是yes還是no,或是其它信息
        3、寫一個腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其它文件類型)
        4、寫一個腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否為正整數
  
       答案:1
       [root@centos7 bin]# cat useradd.sh 
        #!/bin/bash
        read -p "please input the username:" username
        if id -u $username &>/dev/null ;then
	    echo "the user $username exit"
        else 
	        useradd $username
        echo "useradd success,userID is `id -u $username`"
	
        fi
        [root@centos7 bin]# 
        
        答案:2
        [root@centos7 bin]# cat caseyesorno.sh 
        #!/bin/bash
        read -p  "please input yes or no:" yn
        case  $yn in
        yes|y|0|Y|[yY][Ee][Ss])
        echo "you input is: $yn"
        ;;

        no|[Nn][Oo]|N|n)
	echo "you input is:$yn"
        ;;
        *)
	echo "you input is other"
        esac
        [root@centos7 bin]# 
        
        
        
        [root@centos7 bin]# cat  yesorno.sh 
        #!/bin/bash
        read -p  "please input yes or no:" yn
        if   [[ $yn =~ [yY][eE][sS] ||[Yy] ]] ;then
            echo "you input is: yes"
        elif [[ $yn =~ [nN][oO] || [Nn] ]];then
	    echo "you input is:no"
        else 
	    echo "you input is other"
        fi
        [root@centos7 bin]#
        

    答案:3
        [root@centos7 bin]# cat filetype.sh 
            #!/bin/bash
            read -p "please input filename:" filename
            [ !  -e $filename ] && echo "filename not exit"&&exit
            if [ -h $filename ] ;then                                    注意:軟鏈接文件也被識別為普通文件,正確判斷出軟鏈接文件,將-h條件放到-f文件前去判斷
	        echo "filetype is symolink  file"
            elif [ -d $filename ] ;then
        	echo "filetype is dirction"
            elif [ -f $filename ] ;then
	        echo "filetype is common file"
            else 
	        echo "filetype is other"
            fi
            [root@centos7 bin]#
        答案:4
        
            [root@centos7 bin]# cat checkint.sh 
            #!/bin/bash
            read -p "please input a num:" num
            if [ $num -lt 1 ] ;then
	        echo "please input a init"
            elif [[ $num =~  ^[[:digit:]]+$ ]];then 
        	echo "you input is a int"
            else 
	        echo "input error" 
            fi
            [root@centos7 bin]#

    

    

        

原創文章,作者:wangnannan,如若轉載,請注明出處:http://www.www58058.com/35866

(0)
wangnannanwangnannan
上一篇 2016-08-18 10:10
下一篇 2016-08-18 10:10

相關推薦

  • bash之腳本入門及課后作業

    概述: shell是一個工具程序,在用戶登錄后后系統啟動,它解釋并運行由命令或腳本文件輸入的命令,從而實現用戶與內核之間的交互。bash是Linux操作系統上默認的shell,在交互和編程兩方面,bash提供了許多Bourne shell沒有的功能,并且還結合了C shell和Kron shell最有用的功能。 bash的語法和結構   …

    Linux干貨 2016-08-15
  • 廣域網中的DNS服務

    拓撲圖                      創建主DNS:     配置文件/etc/named.conf&nbs…

    2017-04-15
  • Linux Yum源的安裝配置??

    Linux Yum源的安裝配置     一、基本概念   Yum(全稱為YellowdogUpdater,Modified)是一個在Fedora和RedHat以及SUSE、CentOS中的Shell前端軟件包管理器。基于RPM包管理,能夠從指定的服務器自動下載RPM包并且安裝,可以自動處理依賴性關系,并且一次安裝所有依賴的軟件包…

    Linux干貨 2015-05-11
  • htop/vmstat/dstat/ps命令的使用

    Linux htop/vmstat/dstat/ps命令的使用 htop命令 htop工具在系統光盤這中是沒有的,所以要下載的小伙伴們要自己創建yum倉庫通過epel 安裝 創建yum倉庫 vim /etc/yum.repos.d/epel.repo [epel] name=Fedora EPEL baseurl=https://mirrors.tuna.t…

    2017-08-28
  • 測試

    3、查詢file.txt以abc結尾的行grep "abc$" file.txt4、刪除file.txt文件中的空行grep -v '^$' file.txt > file.txtsed '/^$/d' file.txt > file.txt(覆蓋重定向文件變為空該文件為空,但是追加時正常成功。…

    Linux干貨 2016-06-12
  • CA證書服務搭建與申請

    服務端根CA創建證書 進入固定目錄,創建所需要的文件 cd /etc/pki/CA/ touch /etc/pki/CA/index.txt 生成證書索引數據庫文件 echo 01 > /etc/pki/CA/serial 指定第一個頒發證書的序列號 生成秘鑰 (umask 066;openssl genrsa -out /etc/pki/CA/pri…

    2017-09-11
欧美性久久久久