shell腳本2——順序選擇語句

流程控制

     順序執行

     選擇執行

     循環執行

順序執行:

    條件選擇:if語句

if語句為選擇執行

注意:if語句可嵌套

單分支

if  判斷條件:then
    條件為真的分支代碼
fi

雙分支

if  判斷條件; then
    條件為真的分支代碼
else
    條件為假的分支代碼
fi

多分支

if  CONDITION1 ; then
    if-true
elif CONDITION2 ; then
    if-ture
elif CONDITION3 ; then
    if-ture
...
else
    all-false
fi

    條件判斷:case語句

case 變量引用 in
    PAT1)
        分支1
        ;;
    PAT2)
        分支2
        ;;
    ...
    *)
        默認分支
        ;;
esac

case 支持glob 風格的通配符:

    *:任意長度任意字符

    ?:任意單個字符

    []:指定范圍內的任意單個字符

    a|b:a或b

練習:

1 、寫一個腳本/root/bin/createuser.sh ,實現如下功能:使用一個用戶名做為參數,如果指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息

#!/bin/bash
#descriptio
#version 0.1
#author gm
#date 20160812
if `id $1 &> /dev/null`;then
    echo "$1 user is exist;"
else
    useradd $1
    echo "useradd $1."
    echo "$1 `id $1`"
fi

[root@CentOS6 bin]# createuser.sh  gao
gao user is exist;
[root@CentOS6 bin]# createuser.sh  test
useradd test.
test uid=522(test) gid=522(test) groups=522(test)

2 、寫一個腳本/root/bin/yesorno.sh ,提示用戶輸入yes或no, 并判斷用戶輸入的是yes 還是no, 或是其它信息

#!/bin/bash
#description
#version 0.2
#author gm
#date 20160812
read -p "please input yes or no: " string
case $string in
    [yY]|[yY][eE][sS])
        echo "user input is yes.";;
    [nN]|[nN][oO])
        echo "user input is no";;
    *)
        echo "user input is other";;
esac

[root@CentOS6 bin]# yesorno.sh
please input yes or no: yse
user input is other
[root@CentOS6 bin]# yesorno.sh
please input yes or no: yes
user input is yes.
[root@CentOS6 bin]# yesorno.sh
please input yes or no: y
user input is yes.

3 、寫一個腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其它文件類型)

#!/bin/bash
#description
#version 0.1
#author gm
#date 20160812
read -p "please file path: " path
if [ ! -e $path ] ;then
    echo "$path is not exist."
elif [ -h $path ] ;then
    echo "$path is link file."
elif [ -f $path ] ;then
    echo "$path is common file."
elif [ -d $path ] ;then
    echo "$path is dir file"
else
    echo "$path is other file."
fi

[root@CentOS6 bin]# filetype.sh
please file path: /etc
/etc is dir file
[root@CentOS6 bin]# filetype.sh
please file path: /dev/sda
/dev/sda is other file.
[root@CentOS6 bin]# filetype.sh
please file path: /etc/fstab
/etc/fstab is common file.

4 、寫一個腳本/root/bin/checkint.sh, 判斷用戶輸入的參數是否為正整數

#!/bin/bash
#description
#version 0.2
#author gm
#date 20160812
read -p "please ont number of int: " num
testnum=$num
echo $num | grep -qE "^[0-9]+$"
if [ $? -eq 0 ] ; then
    if [ $num -ne 0 ] ; then
        echo "$num is positive integer."
    else
        echo "$num in not positive integer."
    fi
else
    echo "$num is not positive integer."
fi

[root@CentOS6 bin]# checkint.sh
please ont number of int: 123
123 is positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: sdfj
sdfj is not positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: 1.123324
1.123324 is not positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: -123.123
-123.123 is not positive integer.

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

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

相關推薦

  • N26-第六周博客

    vim編輯器及簡單shell腳本示例 請詳細總結vim編輯器的使用并完成以下練習題 1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; [root@localhost tmp]# vim rc.sysinit:%s@^[[:space:]]\+[^[:s…

    系統運維 2017-02-16
  • 磁盤分區知識總結

    Linux中df命令的功能是用來檢查linux服務器的文件系統的磁盤空間占用情況??梢岳迷撁顏慝@取硬盤被占用了多少空間,目前還剩下多少空間等信息。 1.命令格式: df [選項] [文件] 2.命令功能: 顯示指定磁盤文件的可用空間。如果沒有文件名被指定,則所有當前被掛載的文件系統的可用空間將被顯示。默認情況下,磁盤空間將以&nbsp…

    Linux干貨 2017-08-19
  • (總結)MySQL自帶的性能壓力測試工具mysqlslap詳解

    PS:今天一同事問我有木有比較靠譜的mysql壓力測試工具可用。其實mysql自帶就有一個叫mysqlslap的壓力測試工具,還是模擬的不錯的。下面舉例說說。mysqlslap是從5.1.4版開始的一個MySQL官方提供的壓力測試工具。通過模擬多個并發客戶端訪問MySQL來執行壓力測試,同時詳細的提供了“高負荷攻擊MySQL”的數據性能報告。并且能很好的對比…

    Linux干貨 2015-02-10
  • Nginx 代理和緩存

    一 實驗環境 Nginx 版本:nginx-1.8.1 Nginx代理服務器WAN:192.168.1.5 LAN:172.16.2.1 Web1:172.16.2.2 Web2:172.16.2.3 1.  配置好IP、DNS 、網關,確保使用遠程連接工具能夠連接服務器 2.      …

    Linux干貨 2016-12-05
  • N25-第一周博客

      第一周博客作業內容 1.描述計算機的組成及其功能 2.按系列羅列Linux的發行版,并描述不同發行版之間的聯系與區別 3.描述Linux哲學思想,并按照自己的理解對其進行解釋性描述。 4.說明Linux系統上命令的使用格式:詳細介紹,ifconfg,echo,tty,startx,export pwd,history,shutdown,powe…

    Linux干貨 2016-12-03
  • Linux程序包管理(rpm、yum、make)

    linux系統程序安裝的方法有rpm yum 以及make手動編譯3種方法: rpm這個機制最早由Redhat公司開發出來,后來由于實在好用,所以被很多發行版所使用作為軟件安裝的管理方式。不過由于使用RPM安裝軟件時有時會涉及到文件的依賴信,此時需要手動去逐個安裝被依賴的包操作起來十分復雜,于是yum這種線上升級的機制便出現了,它會自己主動解決各文件的依賴關…

    Linux干貨 2017-10-02
欧美性久久久久