#!/bin/sh#測試各種字串比較操作。#shell中對變數的值添加單引號,爽引號和不添加的區別:對類型來說是無關的,即不是添加了引號就變成了字串類型,#單引號不對相關量進行替換,如不對$符號解釋成變數引用,從而用對應變數的值替代,雙引號則會進行替代#author:tenfyguoA="$1"B="$2"echo "輸入的原始值:A=$A,B=$B"#判斷字串是否相等if [ "$A" = "$B" ];thenecho "[ = ]"fi#判斷字串是否相等,與上面的=等價if [ "$A" == "$B" ];thenecho "[ == ]"fi#注意:==的功能在[[]]和[]中的行為是不同的,如下#如果$a以”a”開頭(模式比對)那麼將為true if [[ "$A" == a* ]];thenecho "[[ ==a* ]]"fi#如果$a等於a*(字元匹配),那麼結果為trueif [[ "$A" == "a*" ]];thenecho "==/"a*/""fi#File globbing(通配) 和word splitting將會發生, 此時的a*會自動匹配到對應的當前以a開頭的檔案#如在當前的目錄中有個檔案:add_crontab.sh,則下面會輸出ok#if [ "add_crontab.sh" == a* ];then #echo "ok"#fiif [ "$A" == a* ];thenecho "[ ==a* ]"fi#如果$a等於a*(字元匹配),那麼結果為trueif [ "$A" == "a*" ];thenecho "==/"a*/""fi#字串不相等if [ "$A" != "$B" ];thenecho "[ != ]"fi#字串不相等if [[ "$A" != "$B" ]];thenecho "[[ != ]]"fi#字串不為空白,長度不為0if [ -n "$A" ];thenecho "[ -n ]"fi#字串為空白.就是長度為0.if [ -z "$A" ];thenecho "[ -z ]"fi#需要轉義<,否則認為是一個重新導向符號if [ $A /< $B ];thenecho "[ < ]" fiif [[ $A < $B ]];thenecho "[[ < ]]" fi#需要轉義>,否則認為是一個重新導向符號if [ $A /> $B ];thenecho "[ > ]" fiif [[ $A > $B ]];thenecho "[[ > ]]" fi