getopts

getopts命令用于Shell脚本中解析位置参数。通过指定选项字符串,getopts能够识别带有或不带参数的选项,并将它们存储在指定的变量中。本文详细介绍了getopts的工作原理、错误报告方式及如何在Shell脚本中正确使用它。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


getopts optstring name [args]
              getopts  is used by shell procedures to parse positional parame‐
              ters.  optstring contains the option  characters  to  be  recog‐
              nized;  if  a  character  is  followed by a colon, the option is
              expected to have an argument, which should be separated from  it
              by  white space.  The colon and question mark characters may not
              be used as option characters.  Each time it is invoked,  getopts
              places  the next option in the shell variable name, initializing
              name if it does not exist, and the index of the next argument to
              be processed into the variable OPTIND.  OPTIND is initialized to
              1 each time the shell or a shell script  is  invoked.   When  an
              option  requires  an argument, getopts places that argument into
              the variable OPTARG.  The shell does not reset OPTIND  automati‐
              cally;  it  must  be  manually  reset  between multiple calls to
              getopts within the same shell invocation if a new set of parame‐
              ters is to be used.

              When  the  end  of  options is encountered, getopts exits with a
              return value greater than zero.  OPTIND is set to the  index  of
              the first non-option argument, and name is set to ?.

              getopts  normally  parses the positional parameters, but if more
              arguments are given in args, getopts parses those instead.

             getopts can report errors in two ways.  If the  first  character
              of  optstring  is  a  colon, silent error reporting is used.  In
              normal operation diagnostic messages are  printed  when  invalid
              options  or  missing  option  arguments are encountered.  If the
              variable OPTERR is set to 0, no  error  messages  will  be  dis‐
              played, even if the first character of optstring is not a colon.

              If an invalid option is seen, getopts places ? into name and, if
              not silent, prints an  error  message  and  unsets  OPTARG.   If
              getopts  is  silent,  the  option  character  found is placed in
              OPTARG and no diagnostic message is printed.

              If a required argument is not found, and getopts is not  silent,
              a  question  mark  (?) is placed in name, OPTARG is unset, and a
              diagnostic message is printed.  If getopts  is  silent,  then  a
              colon  (:)  is  placed  in  name and OPTARG is set to the option
              character found.

              getopts returns true if an option, specified or unspecified,  is
              found.  It returns false if the end of options is encountered or
              an error occurs.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值