欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux Shell學(xué)習(xí)筆記第三天

 更新時(shí)間:2010年12月25日 17:56:15   作者:  
今天收獲還是比較多的。 半個(gè)小時(shí)的教程看了將近3個(gè)小時(shí)

第三天:條件選擇

大 綱

    應(yīng)用實(shí)例分析

    條件測(cè)試

    if…else…fi

    case…in…esac

實(shí)現(xiàn)功能菜單

執(zhí)行腳本后

按1,顯示當(dāng)前時(shí)間

按2,顯示CPU負(fù)載

按3,顯示剩余內(nèi)存

按0,退出腳本

按其他字符,提示超出選擇范圍后退出

分析步驟。

    #date +%T

    uptime awk截取

    free –m

條件測(cè)試格式

    #test –option obj

    #[ -option obj ]

返回結(jié)果

  1. 表達(dá)式內(nèi)容測(cè)試結(jié)果是真的
  2. 表達(dá)式內(nèi)容測(cè)試結(jié)果是假的

 

測(cè)試的對(duì)象分類(lèi)

    執(zhí)行結(jié)果(執(zhí)行成功或失?。?/P>

    文件(文件是否存在等)

    文本(是否一致)

    數(shù)字(數(shù)值比較)

條件測(cè)試的選項(xiàng)

選項(xiàng) 作用
-d 目錄
-e 是否存在
-f 是否是普通文件
-s 文件大小是否等于0
-r 是否可讀
-w 是否可寫(xiě)
-x 是否可執(zhí)行

 

邏輯操作符號(hào)

選項(xiàng)

作用

-a

與操作

-o

或操作

!

非操作

 

實(shí)例:

    #test –e /etc/passwd –a –e /etc/shadow 中間是a與操作,則都為0才得0

    #test –e /etc/passwd –o –e /etc/groups 中間是o或操作,則有一真則真0

字符串操作符

    ==            兩個(gè)字符串相等

    !=            兩個(gè)字符串不相等

    -z            空字符串

    -n            非空字符串

實(shí)例:

    #test –z $LOGNAME

    #echo $LOGNAME

    #echo $?

數(shù)值比較操作符

符號(hào)

說(shuō)明

-eq

等于

-ne

不等于

-gt

大于

-lt

小于

-ge

大于等于

-le

小于等于

 

if…else…fi 條件選擇

    if控制結(jié)構(gòu)的基本格式:

        if條件            #判斷開(kāi)始                    可以是命令,也可以是test語(yǔ)句

        then                #如果條件為真                反值真0則執(zhí)行

            命令1        #執(zhí)行命令1

        else                #如果條件為假                反值假1則執(zhí)行

            命令2        #執(zhí)行命令2

        fi                #判斷結(jié)束

    實(shí)例(if…else…fi)1

    inputtest.sh

    #!/bin/bash

    #input test

    echo –n “Enter your name:”

    read name

    #did the user just hit return

    if [ "${name}" == "" ]

    then

        echo “You did not enter any information”

    else

        echo “Your name: ${name}”

    fi

    

 

實(shí)例(if…else…fi)2

filecopy.sh

#!/bin/bash

#file copy

if cp /etc/passwd passwd.bak 2>/dev/null                2>/dev/null 丟掉錯(cuò)誤提示

then

    echo “Good Copy!”

else

    echo “`basename $0`: error,could not copy”

fi

if…else…fi的嵌套 (兩層的嵌套)

    if 條件1;then

        if 條件2;then

            命令1

        else

            命令2

    else

        if條件3;then

            命令3

        else

            命令4

    fi

case…in…esac條件選擇 (比較靈活的方式)

    case語(yǔ)句多用于較多分支判斷

    case格式: (多種模式,只匹配和variable相等的模式)

        case variable in

            模式1)命令1…;;

            模式2)命令2…;;

        esac

    匹配模式

*        匹配任意字符

?        匹配任意單字符

[]        匹配字符范圍

 

 

case…in.esac實(shí)例1

    #!/bin/bash

    #case select

    echo –n “enter a number from 1 to 5:”

read NUM

case $NUM in

        1) echo “you select 1″ ;;

        2) echo “you select 2″ ;;

        3) echo “you select 3″ ;;

        4) echo “you select 4″ ;;

        5) echo “you select 5″ ;;

        *) echo “basename $This is not between 1 and 5″

    esac

case…in.esac實(shí)例2

    題目是:學(xué)生的考試成績(jī)是0-100分,在85以上的要提示you are the best!,在70-84顯示you get a good mark! ,在60-74的顯示come on!,60分以下顯示You must study hard!

#!/bin/bash

echo –n “please input your mark:”

read mark

case $mark in

    100|9[0-9]|8[5-9]) echo “you are the best!”;;        100、90-99、85-89

    8[0-4]|7[0-9]) echo “you get a good mark!”;;        80-84、70-79

    7[0-4]|6[0-9]) echo “come on!”;;                    70-74、60-69

    [0-5][0-9]) echo “You must study hard!”;;            00-59

esac

解決今天的問(wèn)題

    使用if…else…fi的方式對(duì)輸入的變量進(jìn)行判斷。

    在每個(gè)判斷的分支上執(zhí)行相應(yīng)的語(yǔ)句。

    menu.sh

    #!/bin/bash

    clear

    echo “——————–menu—————–”

    echo “1) Show Time”

    echo “2) CPU load”

    echo “3) Memory free”

    echo “0) Exit”

    echo “——————————————–”

    echo -n “Enter you chose [0-3]:”

    read NUM

    

    if [ ${NUM} -lt 0 -o ${NUM} -gt 3 ]

    then

        echo “This is not between 0-3.”

    else

        if [ "${NUM}" == "1" ]

        then

            echo “`date +%T`”

        else

            if [ "${NUM}" == "2" ]

        then

            echo “`uptime | awk -F ‘[,:]‘ ‘{print $7}'`”

            else

            if [ "${NUM}" == "3" ]

            then

            echo “`free -m | awk ‘$1==”Mem:”{print $4}'`”

            else

                exit

            fi

        fi

        fi

    fi

本節(jié)課回顧:

    條件測(cè)試的類(lèi)型

        文件測(cè)試

        文本測(cè)試

        數(shù)值測(cè)試

        邏輯測(cè)試

    if…else…fi條件選擇結(jié)構(gòu)

    case…in…esac

課后測(cè)試

1、修改menu.sh 采用非菜單式,參數(shù)傳遞方式來(lái)進(jìn)行選擇。 例如 #./menu.sh 1 輸出時(shí)間

2、使用case方式來(lái)實(shí)現(xiàn)該菜單選擇方式

Sudu答案:(難免會(huì)有錯(cuò)誤,但是可以實(shí)現(xiàn)成功)

1、修改menu.sh后得出

    #!/bin/bash        

if [ $1 -lt 0 -o $1 -gt 3 ]

then

        echo “This is not between 0-3.”

else

        if [ "$1" == "1" ]

        then

            echo “`date +%T`”

        else

            if [ "$1" == "2" ]

            then

                echo “`uptime | awk -F ‘[,:]‘ ‘{print $7}'`”

            else

                if [ "$1" == "3" ]

                then

            echo “`free -m | awk ‘$1==”Mem:”{print $4}'`”

            else

                exit

            fi

        fi

        fi

fi

2、    #!/bin/bash

clear

echo “——————–menu—————–”

echo “1) Show Time”

echo “2) CPU load”

echo “3) Memory free”

echo “0) Exit”

echo “——————————————–”

echo -n “Enter you chose [0-3]:”

read NUM

case $NUM in

        1) date +%T;;

        2) uptime | awk -F ‘[,:]‘ ‘{print $7}';;

        3) free -m | awk ‘$1==”Mem:”{print $4}';;

        0) exit ;;

        *) echo “This is not between 0-3.” ;;

esac

今天收獲還是比較多的。 半個(gè)小時(shí)的教程看了將近3個(gè)小時(shí)。

雖然說(shuō)if…else…fi比較容易理解,但是用case感覺(jué)簡(jiǎn)單很多,呵呵,看個(gè)人喜好吧。

每天看來(lái)看一節(jié)教程就足夠了。 看多了頭也會(huì)暈的。 呵呵。 繼續(xù)學(xué)習(xí)吧~

相關(guān)文章

最新評(píng)論