getopts
- [root@localhost ~]# cat test.sh
- #!/bin/bash
- getopts a OPT
- echo $OPT
- [root@localhost ~]# ./test.sh -a
- a
- [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
- [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
- [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