今天放仿照書(shū)上寫(xiě)了一個(gè)Shell,總結(jié)如下:
1、定義變量時(shí),等號(hào)兩邊不能空白。即name = "$1" 這種標(biāo)準(zhǔn)的Java寫(xiě)法是錯(cuò)誤的,必須寫(xiě)成
   name="$1",否則name就被當(dāng)成是命令,而非變量
2、傳遞進(jìn)來(lái)的參數(shù)使用"$i"即"$1"、"$2"來(lái)提取第i個(gè)變量,"$0"個(gè)是命令本身,而"$#"表示參數(shù)
   的個(gè)數(shù)。
3、if的判斷語(yǔ)句[ ]中,判斷條件必須和"["或"]"有空白隔開(kāi)即 if [ "$#" != 1 ] 而不能["$#" !=1]
4、如果執(zhí)行命令則命令寫(xiě)在"`"中,而不是"'"中,這點(diǎn)要特別注意,"`"符和"~"通常是同一個(gè)鍵,在
   鍵盤(pán)的左上方位置。
5、追蹤shell的執(zhí)行過(guò)程命令:sh -x hello.sh。執(zhí)行此命令后,執(zhí)行的結(jié)果將變量全部替代為執(zhí)行值。
6、讀入和使用變量:
    read -p "Please input your first name: " firstname
    read -p "Please input your last name:  " lastname
    echo -e "\nYour full name is: $firstname $lastname"
7、數(shù)值運(yùn)算:
    read -p "first number:  " firstnu
    read -p "second number: " secnu
    total=$(($firstnu*$secnu))
    另外一種方式:
    declare -i total=$firstnu*$secnu 
    雙括號(hào)和定義整型都可以做到數(shù)值的運(yùn)算功能
8、利用 test 指令的測(cè)試功能
    test -e /dmtsai && echo "exist" || echo "Not exist"
    
測(cè)試的標(biāo)志 代表意義
1. 關(guān)于某個(gè)檔名的‘類型’偵測(cè)(存在與否),如 test -e filename
-e 該‘檔名’是否存在?(常用)
-f 該‘檔名’是否為檔案(file)?(常用)
-d 該‘檔名’是否為目錄(directory)?(常用)
-b 該‘檔名’是否為一個(gè) block device 裝置?
-c 該‘檔名’是否為一個(gè) character device 裝置?
-S 該‘檔名’是否為一個(gè) Socket 檔案?
-p 該‘檔名’是否為一個(gè) FIFO (pipe) 檔案?
-L 該‘檔名’是否為一個(gè)連結(jié)檔?
2. 關(guān)于檔案的權(quán)限偵測(cè),如 test -r filename
-r 偵測(cè)該檔名是否具有‘可讀’的屬性?
-w 偵測(cè)該檔名是否具有‘可寫(xiě)’的屬性?
-x 偵測(cè)該檔名是否具有‘可執(zhí)行’的屬性?
-u 偵測(cè)該檔名是否具有‘SUID’的屬性?
-g 偵測(cè)該檔名是否具有‘SGID’的屬性?
-k 偵測(cè)該檔名是否具有‘Sticky bit’的屬性?
-s 偵測(cè)該檔名是否為‘非空白檔案’?
3. 兩個(gè)檔案之間的比較,如: test file1 -nt file2
-nt (newer than)判斷 file1 是否比 file2 新
-ot (older than)判斷 file1 是否比 file2 舊
-ef 判斷 file2 與 file2 是否為同一檔案,可用在判斷 hard link 的判定上。 主要意義在判定,兩個(gè)檔案是否均指向同一個(gè) inode 哩!
4. 關(guān)于兩個(gè)整數(shù)之間的判定,例如 test n1 -eq n2
-eq 兩數(shù)值相等 (equal)
-ne 兩數(shù)值不等 (not equal)
-gt n1 大于 n2 (greater than)
-lt n1 小于 n2 (less than)
-ge n1 大于等于 n2 (greater than or equal)
-le n1 小于等于 n2 (less than or equal)
5. 判定字串的資料
test -z string 判定字串是否為 0 ?若 string 為空字串,則為 true
test -n string 判定字串是否非為 0 ?若 string 為空字串,則為 false。
注: -n 亦可省略
test str1 = str2 判定 str1 是否等于 str2 ,若相等,則回傳 true
test str1 != str2 判定 str1 是否不等于 str2 ,若相等,則回傳 false
6. 多重條件判定,例如: test -r filename -a -x filename
-a (and)兩狀況同時(shí)成立!例如 test -r file -a -x file,則 file 同時(shí)具有 r 與 x 權(quán)限時(shí),才回傳 true。
-o (or)兩狀況任何一個(gè)成立!例如 test -r file -o -x file,則 file 具有 r 或 x 權(quán)限時(shí),就可回傳 true。
! 反相狀態(tài),如 test ! -x file ,當(dāng) file 不具有 x 時(shí),回傳 true
    shell中使用舉例:
    echo -e "The program will show you that filename is exist which input by you.\n\n"
    read -p "Input a filename : " filename
    test -z $filename && echo "You MUST input a filename." && exit 0
    # 2. 判斷檔案是否存在?
    test ! -e $filename && echo "The filename $filename DO NOT exist" && exit 0
    # 3. 開(kāi)始判斷檔案類型與屬性
    test -f $filename && filetype="regulare file"
    test -d $filename && filetype="directory"
    test -r $filename && perm="readable"
    test -w $filename && perm="$perm writable"
    test -x $filename && perm="$perm executable"
    # 4. 開(kāi)始輸出資訊!
    echo "The filename: $filename is a $filetype"
    echo "And the permission are : $perm"

9、利用判斷符號(hào) [ ]
    read -p "Please input (Y/N): " yn
    [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0
    [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0
    echo "I don't know what is your choise" && exit 0
    注意,在[ ]中的變量最好使用""括起來(lái),否則容易出錯(cuò)
    舉例來(lái)說(shuō),假如我設(shè)定了 name="VBird Tsai" ,然后這樣判定:
[root@linux ~]# name="VBird Tsai"
            [root@linux ~]# [ $name == "VBird" ]
            bash: [: too many arguments
            
    為什么呢?因?yàn)?$name 如果沒(méi)有使用雙引號(hào)刮起來(lái),那么上面的判定式會(huì)變成:
    [ VBird Tsai == "VBird" ]
    而不是我們要的: [ "VBird Tsai" == "VBird" ]
9、shell具體代碼如下:
   #!/bin/sh
   #
   # This is a simple shell program
   #
   # the first line starts with #!,it means to use /bin/sh to explain
   # this scirpt
   # other lines starting with # means comments,bash ignores these
   #

   PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
   export PATH

   name="$1"
   ip="163.26.197.1"
   exacttime=`date +%Y%m%d`

   if [ $# != 1 ]; then
    echo "Usage: $0 [username]"
    exit
   fi

   echo "today is $exacttime, your name is $name, from $ip"
   echo
   echo "Bye-Bye"