2015-10-09 65 views
0

如果一個getopts標誌設置爲true,我該如何根據它做出另一個標誌?如果一個getopts標誌處於活動狀態,如何使另一個標誌以某種方式運行?

option_r=false 
option_v=false 

while getopts 'r:v' option 
do 
case "$option" in 

r) option_r=true 
    *Just to set the flag to true* 
    ;; 

v) option_v=true 
    if *option -r is set to true* 
    then 
    cp -r directory1 directory2 

    if *option -r is set to false* 
    then 
    cp directory1 directory2 (copy normally) 
    ;; 

我希望我能正確解釋自己。

基本上,如果一個標誌打開,我希望這被反映在另一個選項上。如果選項-r處於活動狀態,則遞歸複製,否則正常複製。

在此先感謝!

回答

1

我會這樣做兩個步驟:首先獲取參數,然後應用邏輯。這裏有一個例子:

option_r=false 
option_v=false 
while getopts "rv" opt; do 
    case $opt in 
     r) 
      option_r=true 
      ;; 
     v) 
      option_v=true 
      ;; 
    esac 
done 

if [ "$option_v" = true ] ; then 
    if [ "$option_r" = true ] ; then 
     cp -r directory1 directory2 
    else 
     cp directory1 directory2 
    fi 
fi 
+1

這是因爲'getopts'不關心必要的,這責令選項進來你必須同時迎合'命令-r -v'和'命令-v -r' – rojomoke

相關問題