曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 8347|回复: 1
打印 上一主题 下一主题

[概念] getopts 命令及脚本示例

[复制链接]

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
跳转到指定楼层
楼主
发表于 2009-1-13 11:46:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
getopts

usage : getopts optstring name [args]

getopts is used by shell procedures to parse positional  parameters. optstring  contains  the  option characters to be recognized; 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 automatically;it must be  manually  reset  between  multiple  calls  to getopts within the same shell invocation if a new set of parameters 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 displayed, 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.

getopts 被 shell 程序用来分析位置参数,optstring 包含能被识别可选参数字符;假如一个字符后面跟着一个冒号,则这个选项后面希望得到一个参数---选项与参数间用空格隔开。冒号和问题标记符不能用作可选参数字符。每一次调用,如若还没退出,getopts 就会把下一个“可选参数符”赋给 shell 变量 name 中进行对 name 的初始化,而且下一个参数的索引会被放置在变量 OPTIND 里。OPTIND 在每次调用 shell 或者 shell 脚本时会被初始化为 1 。当一个选项需要一个参数时,getopts 把一个参数值放入 OPTARG 中。shell 不会自动重置 OPTIND;多个参数被使用时,那么在同一个 shell 调用中多次呼叫 getopts,则需要手动重置 OPTIND

当到达最后一个可选参数时,getopts 返回一个比 0 大的值并退出。OPTIND 被设回到没有可选参数时的索引值(1),然后 name 被设为 ? .

getopts 正常分析位置参数,但如果在 args 中给出了更多的参数则 getopts 就改为分析这些它们。

getopts 报告错误有两种方式。如果 optstring 中的第一个字符是冒号,那么使用静默的错误报告方式(不显示的提示出来)。在正常的操作中,当遇到无效的可选项或者丢失了可选项的参数,那么就会把诊断信息报告出来。如果变量 OPTERR 被设为 0,那么将没有错误的信息被报告出来,即使在 optstring 的第一个字符不是一个冒号(是别的无效选项)。

如果遇到一个无效的可选项,getopts 置 ? 到 name 中,如果不是在静默方式下,则打印一个错误信息,并且不会设置 OPTARG. 如果 getopts 处于静默方式下,可选项字符被置于 OPTARG 中,并且不会打印诊断信息。

如果需求的参数没找到并且 getopts 不处于静默模式,则一个问号(?)被设置在 name 中,OPTARG 不会被设置,此时发出一个诊断信息。如果 getopts 处于静默模式,则一个冒号(:)被置在 name 中,并且 OPTARG 被设置为丢掉参数的可选项字符。

一个可选项不管是指定或者是不指定的,只要发现,getopts 都返回真。如果可选项被检查完或者遇到错误,getopts 则返回假。

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
沙发
 楼主| 发表于 2009-1-13 21:09:28 | 只看该作者

getopts 脚本示例

示例代码
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false

echo ""
echo " \\$OPTIND 被初始化的值为 $OPTIND "
echo ""

while getopts ahfvc: OPTION
do
        case $OPTION in

        a)
                ALL=true
                echo "-a used"
                echo "ALL IS $ALL"
                echo "索引值 \\$OPTIND 此时为 $OPTIND"
                echo ""
                ;;
        h)
                HELP=true
                echo "-h used"
                echo "HELP is $HELP"
                echo "索引值 \\$OPTIND 此时为 $OPTIND"
                echo ""
                ;;
        f)
                FILE=true
                echo "-f used"
                echo "FILE is $FILE"   
                echo "索引值 \\$OPTIND 此时为 $OPTIND"
                echo ""
        c)
                c=$OPTARG
                echo "c value is $c"
                echo "索引值 \\$OPTIND 此时为 $OPTIND"
                echo ""
                ;;

        \\?)
                echo "`basename $0` -[a h f v] -[c value] file"
                ;;

        esac
done

执行情况
[beyes@localhost getopts_dir]$ ./getopts-tst.sh -a -h -c duoduo

 $OPTIND 被初始化的值为 1

-a used
ALL IS true
索引值 $OPTIND 此时为 2

-h used
HELP is true
索引值 $OPTIND 此时为 3

c value is duoduo
索引值 $OPTIND 此时为 5
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-14 16:13 , Processed in 0.080031 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表