== 【需求】 == 因專案需求,需要撰寫一可產生虛擬機器的 shell script,初步設計如下: {{{ $ sudo ./drbl_PXE_PV-VM_create.sh Usage: drbl_PXE_PV-VM_create.sh options Options: -v vm name -c cpu number -r ram size -h deploy vm to which host Examples: drbl_PXE_PV-VM_create.sh options -v drbl110 -c 2 -r 512 -h drbl101 }}} ---- == 【問題】 == 但如何分析各別的參數呢? ---- == 【尋找方法】 == 1. 參考了老大的 /opt/drbl/bin/drbl-doit {{{ #!sh # Parse command while [ $# -gt 0 ]; do case "$1" in -l|--language) shift if [ -z "$(echo $1 |grep ^-.)" ]; then # skip the -xx option, in case specified_lang="$1" shift fi ;; -w|--wol) shift; mode="WOL" ;; -h|--hosts) LIST_HOST="on" shift if [ -z "$(echo $1 |grep ^-.)" ]; then # skip the -xx option, in case IP_LIST="$1" shift fi ;; -u|--user) shift if [ -z "$(echo $1 |grep ^-.)" ]; then # skip the -xx option, in case specified_user="$1" shift fi ;; -n|--no-ping|-b|--batch) shift; ping="no" ;; -v|--verbose) shift; verbose="on" ;; -*) echo "${0}: ${1}: invalid option" >&2 usage >& 2 exit 2 ;; *) break ;; esac done }}} * 老大是搭配了 shift 和 positional parameter 的概念 2. [http://unixhelp.ed.ac.uk/scrpt/scrpt2.1.html 令一篇文件的教學]也是跟老大雷同