Shell Scripts(2) - 條件判斷、控制結構數值比對:expression1 -eq expression2ex1 = ex2 expression1 -ne expression2ex1 != ex2 expression1 -gt expression2ex1 > ex2 expression1 -ge expression2ex1 >= ex2 expression1 -lt expression2ex1 < ex2 expression1 -le expression2ex1 <= ex2!expressionexpression為false時,融資回傳true檔案狀況:我們可以使用 if test -f file then ... fi 又或 if [ -f file ] then ... fi 來測試檔案,第二種要注意空格的位置,在符號和條件判斷式之間都要加上空白。 而then的位置如果是在同一行時,必須要在]和then之間再加上;(分號)。 -d file 是否存在且為目錄 -e file 是否存在 -f file 是否存在且為一般檔案 -f file 房屋二胎是否有設定set-group-id -u file 是否有設定set-user-id -r file 是否可讀 -w file 是否可寫-x file 是否可執行-s file 檔案大小是否為非零字串比對:string1 = string2是否相等 string1 != string2是否不相等  -n string 是否不是空字串  - z string是否是空字串 範例:建構一個檔案 second.sh,輸入以下內容:#!#!/bin/sh# 酒店工作This is the second shell scriptecho ------ compare number ------ if [ $1 -gt $2 ]; then        echo "$1 > $2"    elif [ $1 -lt $2 ]; then        echo "$1 < $2"else    echo "$1 = 租屋網$2"    fiecho ------ test file ------ if [ -z "$3" ]; then    echo "The third parameter is needed by program"    exit 1else    if [ -f "$3" ]; then        echo "$3 is common 支票貼現file"    elif [ -d "$3" ]; then        echo "$3 is directory"    elif [ -e "$3" ]; then        echo "$3 exists"    else         echo 婚禮顧問"The file type of $3 is not recognized or not existed"        exit 1                fi    fiecho ------ compare string ------ echo "Is it morning? Please answer yes or no"read timeofdayif seo[ "$timeofday" = "yes" ]; then    echo "Good morning"elif [ "$timeofday" = "no" ]; then    echo "Good afternoon"else    echo "Sorry, $timeofday not recognized. Enter yes or no"    室內裝潢exit 1fi            exit 0說明:這個範例有三個部分,分別是數值比對、檔案測試、字串比對。這個程式需輸入三個參數,第一個和第二個為要比對的數字,第三個為要測試的檔案。執行 ./second.sh 1 2 second.sh------ compare number ------1 < 2------ test file ------get seosecond.shsecond.sh is common file------ compare string ------Is it morning? Please answer yes or noyesGood morningsecond.sh第一部分判斷數值的大小,第二部分檢查檔案是曾存在還有他是目錄或一般檔案,最後使用read讀取使用者輸入,來判斷現在是否為早上。


.msgcontent .wsharing ul li { text-indent: 0; }



分享室內設計

Facebook
Plurk
YAHOO!

arrow
arrow
    全站熱搜

    fv28fvpzpf 發表在 痞客邦 留言(0) 人氣()