Shell case esac囂鞘
<貧匯准
和匯准>
case ... esac 嚥凪麿囂冱嶄議 switch ... case 囂鞘窃貌?頁匯嶽謹蛍乢僉夲潤更。
case 囂鞘謄塘匯倖峙賜匯倖庁塀?泌惚謄塘撹孔?峇佩屢謄塘議凋綜。case囂鞘鯉塀泌和?
函峙繍殊霞謄塘議耽匯倖庁塀。匯稀庁塀謄塘?夸峇佩頼謄塘庁塀屢哘凋綜朔音壅写偬凪麿庁塀。泌惚涙匯謄塘庁塀?聞喘佛催 * 俺資乎峙?壅峇佩朔中議凋綜。
和中議重云戻幣補秘1欺4?嚥耽匯嶽庁塀序佩謄塘?
壅訟匯倖箭徨?
case 囂鞘謄塘匯倖峙賜匯倖庁塀?泌惚謄塘撹孔?峇佩屢謄塘議凋綜。case囂鞘鯉塀泌和?
case 峙 in 庁塀1) command1 command2 command3 ;; 庁塀2? command1 command2 command3 ;; *) command1 command2 command3 ;; esaccase垢恬圭塀泌貧侭幣。函峙朔中駅倬葎購囚忖 in?耽匯庁塀駅倬參嘔凄催潤崩。函峙辛參葎延楚賜械方。謄塘窟孖函峙憲栽蝶匯庁塀朔?凪寂侭嗤凋綜蝕兵峇佩岷崛 ;;。;; 嚥凪麿囂冱嶄議 break 窃貌?吭房頁柳欺屁倖 case 囂鞘議恷朔。
函峙繍殊霞謄塘議耽匯倖庁塀。匯稀庁塀謄塘?夸峇佩頼謄塘庁塀屢哘凋綜朔音壅写偬凪麿庁塀。泌惚涙匯謄塘庁塀?聞喘佛催 * 俺資乎峙?壅峇佩朔中議凋綜。
和中議重云戻幣補秘1欺4?嚥耽匯嶽庁塀序佩謄塘?
echo 'Input a number between 1 to 4' echo 'Your number is:\c' read aNum case $aNum in 1) echo 'You select 1' ;; 2) echo 'You select 2' ;; 3) echo 'You select 3' ;; 4) echo 'You select 4' ;; *) echo 'You do not select a number between 1 to 4' ;; esac補秘音揖議坪否?氏嗤音揖議潤惚?箭泌?
Input a number between 1 to 4 Your number is:3 You select 3
壅訟匯倖箭徨?
#!/bin/bash
option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac
塰佩潤惚?
$./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] $ ./test.sh -f index.htm $ vi test.sh $ ./test.sh -f index.htm File name is index.htm $ ./test.sh -d unix Dir name is unix $
<貧匯准
和匯准>