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
下一篇 2016-08-18

相關推薦

  • LAMP基于RPM包實現(httpd與php以fastcgi方式結合) 2

    概述:     承接上篇,本篇為介紹下利用rpm包,實現LAMP組合,其中httpd和php的結合方式為fastcgi,也就是php運行為獨立的服務,監聽的某個套接字上,接受請求,提供服務     包括LAMP安裝過程(http、php-fpm、mysql-server、php-…

    Linux干貨 2016-10-15
  • Linux網絡管理基礎

    Linux網絡管理基礎 動態路由 Bonding Network Teaming 靜態路由實驗 Linux的網絡管理,了解基本的網絡知識是基礎,除此,要掌握好ifconig命令、ip命令、nmcli命令(CentOS 7),以及涉及到網絡的配置文件。 配置動態路由: 通過守護進程獲取動態路由,安裝quagga包,支持RIP、OSPF、BGP,通過命令vtys…

    Linux干貨 2016-09-09
  • nginx相關配置及解釋

    全局配置: user  nginx nginx; #運行程序的用戶和用戶組pid      /var/run/nginx.pid; #主控進程load_module /usr/lib64/nginx/modules/ngx_http_geoip_module.so;#加載模塊 work進程的數量:通常為當前主…

    Linux干貨 2017-05-07
  • LAMP (php-fpm模式)部署出現的奇葩問題

    1. 安裝環境:(cent6.5) yum install -y php php-devel httpd  php-fpm mysql 2. 修改配置文件 vim /etc/httpd/conf/http.conf 注釋掉DocumentRoot vim /etc/httpd/conf.d/vhost.conf <VirtualHost *:…

    2017-04-10
  • Linux之bash shell腳本編程入門篇(一)

    什么是bash shell腳本編程? 答:Linux里面有多種shell,而CentOS和redhat的默認shell是bash shell。至于shell腳本,這個跟windows操作系統里面的批處理文件有點像(.bat的文件)。不知道大家還是否記得Linux的哲學思想嗎?其中有那么兩點點:由眾多目的的單一應用程序組成:一個程序只做一件事,且做好;組合目的…

    Linux干貨 2016-08-15
  • 高級變量-有類型變量

    一.高級變量用法– 有類型變量   Shell 變量一般是無類型的,但是bash Shell 提供了declare和 typeset 兩個命令用于指定變量的類型,兩個命令是等價的 declare [ 選項]  變量名 -r  將變量設置為只讀屬性 -i  將變量定義為整型數 -a  將變量定義為數…

    Linux干貨 2016-11-24
欧美性久久久久