셸 스 크 립 트 의 매개 변수 옵션
base 셸 의 내 장 된 명령 을 위해 셸 의 인자 확장 을 제공 합 니 다. 셸 스 크 립 트 의 옵션 을 가 져 올 수 있 고 셸 스 크 립 트 옵션 뒤에 있 는 인 자 를 가 져 올 수 있 으 며 스 크 립 트 에서 호출 할 수 있 습 니 다.
getopts optstring name [arg]
1. optstring 스 크 립 트 가 사용 할 수 있 는 매개 변 수 를 정의 합 니 다.
2. 짧 은 옵션 만 지원 하고 긴 옵션 은 지원 하지 않 습 니 다.
3. 옵션 뒤의 인 자 를 사용 하려 면 옵션 뒤에 콜론 (:) 을 추가 하면 됩 니 다.
4. 기본 적 인 상황 에서 하나의 인자 만 가 져 올 수 있 습 니 다.
eg1:
- [root@localhost ~]# cat test.sh
- #!/bin/bash
- getopts a OPT
- echo $OPT
- [root@localhost ~]# ./test.sh -a
- a
* 옵션 뒤의 인 자 를 사용 하려 면 옵션 뒤에 콜론 (:) 을 추가 하면 됩 니 다.
eg2:
- [root@localhost ~]# cat test.sh
- #!/bin/bash
- getopts a: OPT
- echo $OPT
- echo $OPTARG
- [root@localhost ~]# ./test.sh -a "this is a"
- a
- this is a
* 잘못된 정 보 를 출력 하지 않 으 려 면 모든 옵션 에 콜론 (:) 을 추가 하면 됩 니 다.
eg3:
- [root@localhost ~]# cat a.sh
- #!/bin/bash
- getopts a: OPT
- echo $OPT
- echo $OPTARG
- [root@localhost ~]# ./a.sh -b
- ./a.sh: illegal option -- b
- ?
- [root@localhost ~]# cat a.sh
- #!/bin/bash
- getopts :a: OPT
- echo $OPT
- echo $OPTARG
- [root@localhost ~]# ./a.sh -b
- ?
- b
* 여러 옵션 을 사용 하려 면 순환 문 구 를 사용 해 야 합 니 다.
eg4:
- [root@localhost ~]# cat a.sh
- #!/bin/bash
- while getopts ":a:b:" OPT;do
- case $OPT in
- a)
- echo $OPT
- echo $OPTARG
- ;;
- b)
- echo $OPT
- echo $OPTARG
- ;;
- *)
- echo "wrong option"
- ;;
- esac
- done
- [root@localhost ~]# ./a.sh -a "this is a"
- a
- this is a
- [root@localhost ~]# ./a.sh -b "this is b"
- b
- this is b
- [root@localhost ~]# ./a.sh -c "this is c"
- wrong option
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Shell 프로 그래 밍 - 텍스트 처리 grep, sed사용자 가 지정 한 '모드' 에 따라 대상 텍스트 를 일치 하 게 검사 하고 일치 하 는 줄 을 인쇄 합 니 다. ##포함 되 지 않 음, 역방향 일치 \ ##키워드 앞 뒤 가 맞지 않 고 키워드 만 일치 합 니 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.