差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

linux:shell_script:bypass_invalid_options_of_getopts [2021/07/10 17:58]
tony 建立
linux:shell_script:bypass_invalid_options_of_getopts [2023/06/25 09:48]
行 1: 行 1:
-{{tag>​Linux ShellScript}} 
-====== Bypass invalid options of getopts ====== 
-===== Problem ===== 
-我有一隻程式是透過python撰寫,然後透過shellscript去啟動。因為我懶得加功能在python上,所以希望能透過修改shellscript去增加新功能的選項。然而,當我透過getopts去parse輸入參數時,如果少宣告參數在getopts上,程式就會自行停止。本篇文章主要分享pypass的方法。 
-===== How to? ===== 
-預設情況下getopts遇到有問題參數就會停止並顯示無效參數,要避掉這問題解法如下, 
-  - 在getopts的第一個參數最前面加上:​,可以把處理問題流程交給我們自己的腳本。 
-  - 在?​與:​處理流程上執行shift,讓parse動作繼續往下。 
-<code bash> 
-branch=master 
-test_mode=false 
-while getopts ":​b:​t"​ OPT; do 
-    case $OPT in 
-        b) branch="​$OPTARG";;​ 
-        t) test_mode=true;;​ 
-                \?) shift;; 
-                :) shift;; 
-    esac 
-done 
-</​code>​ 
-這樣就可以pypass掉不想處理的項目。 
-===== Reference ===== 
-  * [[https://​stackoverflow.com/​questions/​22909253/​new-to-bash-keep-getting-illegal-option-error/​24868071|new to Bash - keep getting Illegal option error]] 
  
-=====    ===== 
----- 
-\\ 
-~~DISQUS~~