getopt 를 사용 하여 셸 스 크 립 트 를 처리 하 는 인자
지금 은 시스템 자체 도움말 파일 로 getopt 가 bash 에서 사용 하 는 기술 을 말 합 니 다.
- #!/bin/bash
-
- # A small example program for using the new getopt(1) program.
- # This program will only work with bash(1)
- # An similar program using the tcsh(1) script language can be found
- # as parse.tcsh
-
- # Example input and output (from the bash prompt):
- # ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
- # Option a
- # Option c, no argument
- # Option c, argument `more'
- # Option b, argument ` very long '
- # Remaining arguments:
- # --> `par1'
- # --> `another arg'
- # --> `wow!*\?'
-
- # Note that we use `"$@"' to let each command-line parameter expand to a
- # separate word. The quotes around `$@' are essential!
- # We need TEMP as the `eval set --' would nuke the return value of getopt.
- TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
- -n 'example.bash' -- "$@"`
-
- if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
-
- # Note the quotes around `$TEMP': they are essential!
- eval set -- "$TEMP"
-
- while true ; do
- case "$1" in
- -a|--a-long) echo "Option a" ; shift ;;
- -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
- -c|--c-long)
- # c has an optional argument. As we are in quoted mode,
- # an empty parameter will be generated if its optional
- # argument is not found.
- case "$2" in
- "") echo "Option c, no argument"; shift 2 ;;
- *) echo "Option c, argument \`$2'" ; shift 2 ;;
- esac ;;
- --) shift ; break ;;
- *) echo "Internal error!" ; exit 1 ;;
- esac
- done
- echo "Remaining arguments:"
- 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 을 사용 하 는 목적 은 매개 변수 에 셸 명령 이 있 고 잘못된 확장 을 방지 하기 위해 서 입 니 다.실제 정부 에서 제공 하 는 이 문법 도 간결 하지 않 습 니 다. 다음은 제 가 평소에 사용 하 는 것 을 제공 합 니 다.
- ARGS=`getopt -a -o I:D:T:e:k:LMSsth -l instence:,database:,table:,excute:,key:,list,master,slave,status,tableview,help -- "$@"`
- [ $? -ne 0 ] && usage
- #set -- "${ARGS}"
- eval set -- "${ARGS}"
-
- while true
- do
- case "$1" in
- -I|--instence)
- instence="$2"
- shift
- ;;
- -D|--database)
- database="$2"
- shift
- ;;
- -T|--table)
- table="$2"
- shift
- ;;
- -e|--excute)
- excute="yes"
- shift
- ;;
- -k|--key)
- key="$2"
- shift
- ;;
- -L|--list)
- LIST="yes"
- ;;
- -M|--master)
- MASTER="yes"
- ;;
- -S|--slave)
- SLAVE="yes"
- ;;
- -A|--alldb)
- ALLDB="yes"
- ;;
- -s|--status)
- STATUS="yes"
- ;;
- -t|--tableview)
- TABLEVIEW="yes"
- ;;
- -h|--help)
- usage
- ;;
- --)
- shift
- break
- ;;
- esac
- shift
- done
중점적으로 살 펴 보 자. esac 와 done 중의 shift, 그리고 기호 "-"조건 에서 의 shift;break 조작.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
양식 제출 후 제출 버튼 비활성화텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.