getopt 를 사용 하여 셸 스 크 립 트 를 처리 하 는 인자

getopt 명령 은 bash 의 내장 명령 이 아 닙 니 다. util - linux 패키지 에서 제공 하 는 외부 명령 입 니 다.bash 의 내 장 된 명령 에 비해 getopt 는 짧 은 인삼 - s 뿐만 아니 라 - longot 의 긴 매개 변 수 를 지원 하 며 - longot 의 간략화 매개 변 수 를 지원 합 니 다.getopt 는 tcsh 의 다른 셸 에 사용 할 수 있 습 니 다.
지금 은 시스템 자체 도움말 파일 로 getopt 가 bash 에서 사용 하 는 기술 을 말 합 니 다.
 

  
  
  
  
  1. #!/bin/bash  
  2.  
  3. # A small example program for using the new getopt(1) program.  
  4. # This program will only work with bash(1)  
  5. # An similar program using the tcsh(1) script language can be found  
  6. # as parse.tcsh  
  7.  
  8. # Example input and output (from the bash prompt):  
  9. # ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "  
  10. # Option a  
  11. # Option c, no argument  
  12. # Option c, argument `more'  
  13. # Option b, argument ` very long '  
  14. # Remaining arguments:  
  15. # --> `par1'  
  16. # --> `another arg'  
  17. # --> `wow!*\?'  
  18.  
  19. # Note that we use `"$@"' to let each command-line parameter expand to a   
  20. # separate word. The quotes around `$@' are essential!  
  21. # We need TEMP as the `eval set --' would nuke the return value of getopt.  
  22. TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \  
  23.      -n 'example.bash' -- "$@"`  
  24.  
  25. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi  
  26.  
  27. # Note the quotes around `$TEMP': they are essential!  
  28. eval set -- "$TEMP" 
  29.  
  30. while true ; do  
  31.         case "$1" in 
  32.                 -a|--a-long) echo "Option a" ; shift ;;  
  33.                 -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;  
  34.                 -c|--c-long)  
  35.                         # c has an optional argument. As we are in quoted mode,  
  36.                         # an empty parameter will be generated if its optional  
  37.                         # argument is not found.  
  38.                         case "$2" in 
  39.                                 "") echo "Option c, no argument"; shift 2 ;;  
  40.                                 *)  echo "Option c, argument \`$2'" ; shift 2 ;;  
  41.                         esac ;;  
  42.                 --) shift ; break ;;  
  43.                 *) echo "Internal error!" ; exit 1 ;;  
  44.         esac  
  45. done  
  46. echo "Remaining arguments:" 
  47. for arg do echo '--> '"\`$arg'" ; done 

 getopt 명령 의 옵션 설명:
- a getopt 의 긴 매개 변 수 를 "-"기호 로 시작 합 니 다. - l 과 동시에 사용 해 야 합 니 다. - l 뒤에 getopt 지원 긴 매개 변수 목록 - n program 은 getopt 처리 매개 변수 가 오 류 를 되 돌려 주면 누가 이 오 류 를 처 리 했 는 지 알려 줍 니 다. 이것 은 여러 스 크 립 트 를 호출 할 때 유용 합 니 다 - o 뒤에 짧 은 매개 변수 목록 을 연결 합 니 다. 이 용법 은 getopts 와 유사 합 니 다 - u 매개 변수 목록 에 따옴표 를 붙 이지 않 습 니 다. 기본 값 은 따옴표 (- u 옵션 사용 하지 않 음) 입 니 다. 예 를 들 어 따옴표 가 붙 지 않 을 때 -- longopt "select * from db1. table 1"$2 는 완전한 SQL 문장 이 아 닌 select 만 가 져 옵 니 다.
eval 을 사용 하 는 목적 은 매개 변수 에 셸 명령 이 있 고 잘못된 확장 을 방지 하기 위해 서 입 니 다.실제 정부 에서 제공 하 는 이 문법 도 간결 하지 않 습 니 다. 다음은 제 가 평소에 사용 하 는 것 을 제공 합 니 다.
 

  
  
  
  
  1. ARGS=`getopt -a -o I:D:T:e:k:LMSsth -l instence:,database:,table:,excute:,key:,list,master,slave,status,tableview,help -- "$@"`  
  2. [ $? -ne 0 ] && usage  
  3. #set -- "${ARGS}"  
  4. eval set -- "${ARGS}" 
  5.  
  6. while true  
  7. do  
  8.         case "$1" in 
  9.         -I|--instence)  
  10.                 instence="$2" 
  11.                 shift  
  12.                 ;;  
  13.         -D|--database)  
  14.                 database="$2" 
  15.                 shift  
  16.                 ;;  
  17.         -T|--table)  
  18.                 table="$2" 
  19.                 shift  
  20.                 ;;  
  21.         -e|--excute)  
  22.                 excute="yes" 
  23.                 shift  
  24.                 ;;  
  25.         -k|--key)  
  26.                 key="$2" 
  27.                 shift  
  28.                 ;;  
  29.         -L|--list)  
  30.                 LIST="yes" 
  31.                 ;;  
  32.         -M|--master)  
  33.                 MASTER="yes" 
  34.                 ;;  
  35.         -S|--slave)  
  36.                 SLAVE="yes" 
  37.                 ;;  
  38.         -A|--alldb)  
  39.                 ALLDB="yes" 
  40.                 ;;  
  41.         -s|--status)  
  42.                 STATUS="yes" 
  43.                 ;;  
  44.         -t|--tableview)  
  45.                 TABLEVIEW="yes" 
  46.                 ;;  
  47.         -h|--help)  
  48.                 usage  
  49.                 ;;  
  50.         --)  
  51.                 shift  
  52.                 break 
  53.                 ;;  
  54.         esac  
  55. shift  
  56. done 

중점적으로 살 펴 보 자. esac 와 done 중의 shift, 그리고 기호 "-"조건 에서 의 shift;break 조작.

좋은 웹페이지 즐겨찾기