| 1 | == 【需求】 == |
| 2 | 因專案需求,需要撰寫一可產生虛擬機器的 shell script,初步設計如下: |
| 3 | {{{ |
| 4 | $ sudo ./drbl_PXE_PV-VM_create.sh |
| 5 | Usage: drbl_PXE_PV-VM_create.sh options |
| 6 | Options: |
| 7 | -v vm name |
| 8 | -c cpu number |
| 9 | -r ram size |
| 10 | -h deploy vm to which host |
| 11 | Examples: |
| 12 | drbl_PXE_PV-VM_create.sh options -v drbl110 -c 2 -ram 512 -h drbl101 |
| 13 | }}} |
| 14 | ---- |
| 15 | == 【問題】 == |
| 16 | 但如何分析各別的參數呢? |
| 17 | |
| 18 | ---- |
| 19 | == 【尋找方法】 == |
| 20 | 1. 參考了老大的 /opt/drbl/bin/drbl-doit |
| 21 | {{{ |
| 22 | #!sh |
| 23 | # Parse command |
| 24 | while [ $# -gt 0 ]; do |
| 25 | case "$1" in |
| 26 | -l|--language) |
| 27 | shift |
| 28 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 29 | # skip the -xx option, in case |
| 30 | specified_lang="$1" |
| 31 | shift |
| 32 | fi |
| 33 | ;; |
| 34 | -w|--wol) |
| 35 | shift; mode="WOL" ;; |
| 36 | -h|--hosts) |
| 37 | LIST_HOST="on" |
| 38 | shift |
| 39 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 40 | # skip the -xx option, in case |
| 41 | IP_LIST="$1" |
| 42 | shift |
| 43 | fi |
| 44 | ;; |
| 45 | -u|--user) |
| 46 | shift |
| 47 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 48 | # skip the -xx option, in case |
| 49 | specified_user="$1" |
| 50 | shift |
| 51 | fi |
| 52 | ;; |
| 53 | -n|--no-ping|-b|--batch) |
| 54 | shift; ping="no" ;; |
| 55 | -v|--verbose) |
| 56 | shift; verbose="on" ;; |
| 57 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 58 | usage >& 2 |
| 59 | exit 2 ;; |
| 60 | *) break ;; |
| 61 | esac |
| 62 | done |
| 63 | }}} |
| 64 | * 老大是搭配了 shift 和 positional parameter 的概念 |
| 65 | 2. [http://unixhelp.ed.ac.uk/scrpt/scrpt2.1.html 令一篇文件的教學]也是跟老大雷同 |