LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 700|回复: 5

[求助] 对 getopts 的疑问!

[复制链接]
发表于 2006-3-8 22:13:33 | 显示全部楼层 |阅读模式


  1. # filename: getopts-test

  2. while getopts ":mnopq:rs" Option
  3. do
  4.         case $Option in
  5.           m      ) echo "Scenario #1: option -m- [OPTIND=${OPTIND}]";;
  6.           n|o   ) echo "Scenario #2: option -$Option-   [OPTIND=$OPTIND]";;
  7.           p      ) echo "Scenario #3: option -p- [OPTIND=$OPTIND]";;
  8.           q      ) echo "Scenario #4: option -q-\
  9. with argument "$OPTARG"       [OPTIND=$OPTIND]";;
  10.           #  Note that option 'q' must have an associated argument,
  11.           #+ otherwise it falls through to the default.
  12.           r|s    ) echo "Scenario #5: option -$Option-";;
  13.           *      ) echo "Unimplemented option chosen.";;
  14.         esac
  15. done

复制代码



  1. bash# ./getopts-test -mppnn

  2. Scenario #1: option -m- [OPTIND=1]
  3. Scenario #3: option -p- [OPTIND=1]
  4. Scenario #3: option -p- [OPTIND=1]
  5. Scenario #2: option -n- [OPTIND=1]
  6. Scenario #2: option -n- [OPTIND=2]

复制代码


As a matter of fact, I am not sure how this script runs. The variable OPTIND seems not to be that reliable.
 楼主| 发表于 2006-3-9 10:12:09 | 显示全部楼层
Up!Up!Up!
回复 支持 反对

使用道具 举报

发表于 2006-3-9 13:05:30 | 显示全部楼层
abs mentioned that OPTIND is not reliable to tell where the option is coming from. It seems to treat "-mnop" style as a whole and strip down one char a time. If you "-m -n -o -p", OPTIND will be incremental.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-9 15:31:13 | 显示全部楼层
Post by yongjian
abs mentioned that OPTIND is not reliable to tell where the option is coming from. It seems to treat "-mnop" style as a whole and strip down one char a time. If you "-m -n -o -p", OPTIND will be incremental.


Oh, yeah. I found it in abs. A little depressed.

Anyway, thanks for your help, yongjian.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-9 15:43:34 | 显示全部楼层
Post by yongjian
abs mentioned that OPTIND is not reliable to tell where the option is coming from. It seems to treat "-mnop" style as a whole and strip down one char a time. If you "-m -n -o -p", OPTIND will be incremental.


Oh, yeah. I found it in abs. A little depressed.

Anyway, thanks for your help, yongjian.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-9 15:49:30 | 显示全部楼层
Post by yongjian
abs mentioned that OPTIND is not reliable to tell where the option is coming from. It seems to treat "-mnop" style as a whole and strip down one char a time. If you "-m -n -o -p", OPTIND will be incremental.


Oh, yeah. I found it in abs. A little depressed.

Anyway, thanks for your help, yongjian.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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