중첩된 경우 - ";;"를 "esac" 바로 뒤에 배치하지 않고 가능한 명령 뒤에 배치해야 하는 이유는 무엇입니까?

중첩된 경우 - ";;"를 "esac" 바로 뒤에 배치하지 않고 가능한 명령 뒤에 배치해야 하는 이유는 무엇입니까?

;;중첩된 사례 설명에서 종료에 대한 설명을 원합니다 .

어딘가에 문서화되어 있습니까?

왜 다음과 같이 작동하지 않습니까?

     #!/bin/ksh  
     ...    
     esac
     ;; 
     print "why here?"
     ...

하지만 작동 방식은 다음과 같습니다.

#!/bin/ksh

var1="C" 
var2=0

case ${var1} in
  A) print "A"  
  ;;

  B) print "B"
  ;;

  C) print "C"

     case $var2 in
       0)
         print "A B"
         ;;
       1)
         print "C"           
         ;;
     esac
     print "why here?"
     ;;  

  *) print ${var1}
  ;;
esac

답변1

;;별도의 케이스 블록. 그런 다음 쉘이 다른 것을 찾기를 기대하는 것은 무엇입니까?무늬새 사례 블록을 시작하거나 문의 끝을 esac표시합니다 . 가 아니며 유효하지 않습니다.caseprint somethingesac무늬이므로 오류가 발생합니다. 당신이 하나를 원한다면기본/뒤로 물러나다케이스 블록, 사용 *)또는 (*):

case $something in
  (foo) cmd1
        cmd2
        ;; # end of first block
  (bar) cmd3;; # end of second block
  (*)   cmd4 # default case block
esac # note that the ;; is not required before the esac.

명령문이 case중첩되었는지 여부는 관련이 없습니다.

답변2

Bash의 매뉴얼 페이지에 잘 문서화되어 있는 것 같습니다.

발췌

   case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
        A case command first expands word, and tries to match it against 
        each pattern in turn, using the same matching rules as for 
        pathname expansion  (see Pathname Expansion below).  The word is 
        expanded using tilde expansion, parameter and variable expansion, 
        arithmetic substitution, command substitution, process substitution 
        and quote removal.  Each pattern examined is expanded  using  tilde  
        expansion,  parameter  and  variable  expansion,  arithmetic  
        substitution, command substitution, and process substitution.  If 
        the shell option nocasematch is enabled, the match is performed 
        without regard to the case of alphabetic characters.  When a match  
        is  found, the  corresponding list is executed.  

        If the ;; operator is used, no subsequent matches are attempted 
        after the first pattern match. Using ;& in place of ;; causes 
        execution to continue with the list associated with the next set of 
        patterns.  Using ;;& in place  of ;;  causes  the shell to test the 
        next pattern list in the statement, if any, and execute any 
        associated list on a successful match. The exit status is zero if no 
        pattern matches.  Otherwise, it is the exit status of the last 
        command executed in list.

;;기호가 Case ... esac 블록 외부에 있기 때문에 작동하지 않습니다 .

 esac
 ;; 
 print "why here?"

또한 귀하의 예에는 Korn 쉘( ksh)이 표시되어 있지만 기호는 내가 이해하는 것과 동일합니다 ksh. 여기에도 표시되어 있습니다.

인용하다

관련 정보