Linux Gvim shell case...esac語句

#case ..esac語句:與switch ..case相似,是一種多分支選擇結構
#case語句匹配一個值或一個模式,若是匹配成功執行相應命令,若是匹配不成功執行*命令
#;;與break相似,表明跳到整個case語句的最後
#case 值 in
#模式1)
#    command1
#    ;;
#模式2)
#    command1
#    ;;
#*)
#    command1
#    ;;
#esac
#示例以下:html

 1 echo 'Input a number between 1 to 4'
 2 echo -e 'Your number is:\c'
 3 read aNum
 4 case $aNum in
 5         1)  echo 'You select 1'
 6         ;;
 7         2)  echo 'You select 2'
 8         ;;
 9         3)  echo 'You select 3'
10         ;;
11         4)  echo 'You select 4'
12         ;;
13         *)  echo 'You do not select a number between 1 to 4'
14         ;;
15 esac

#測試輸入xxx.sh -f index.html or xxx.sh -d unix 此時進入×)輸出測試

 1 option="${1}"
 2 case ${option} in
 3    -f) FILE="${2}"
 4       echo "File name is $FILE"
 5       ;;
 6    -d) DIR="${2}"
 7       echo "Dir name is $DIR"
 8       ;;
 9    *) 
10       echo "`basename ${0}`:usage: [-f file] | [-d directory]"        #若是都不是,則執行xxx.sh#
11       exit 1 # Command to come out of the program with status 1
12       ;;
13 esac
相關文章
相關標籤/搜索