Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61791
  • 博文数量: 25
  • 博客积分: 1899
  • 博客等级: 上尉
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-29 23:45
文章分类

全部博文(25)

文章存档

2012年(22)

2010年(3)

我的朋友

分类: LINUX

2012-06-09 15:42:42

一、getopt的C接口
man 3 getopt
// 手册给了详细说明,并有了例子。getopt使用了内部全局变量
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;

中文参考
   
,一段好的代码

备注一下这段代码,有几个字是要注意的
(1) $()相当于``

(2) 关于--
**
man find, A double dash -- can also be used to signal that
any remaining arguments are not options
**
man bash, , -- also mean args are _set_ to words after --
**

(3) for o; do
done
没有in语句,上面的语句相当于
for o in "$@"; do
done


(4) getopts是shell内置命令,有
getopts option_string variable
OPTIND:初始化为1, 每次getopts处理完一个opt后++
OPTARG:包含了对应variable的选项的参数的值
options-strings开始部分没有冒号:与有:是有区别的。

(5) getopt没有内置参数
getopt只是剔除所有无效的opt并重新排序,如
getopt -o :abc:d:e -- -a -b myhaha -c mygod -e -d
-a -b -c 'mygod' -e -- 'myhaha'

getopt -o :ab: -l "" -- -a -b myhaha

getopt -l "help,verbose,work-dir:,config-file:,unk,desc-str:" -- --help

(6) 如果getopt命令本身没有使用-o|--option选项的话,那么
--后面的第一个参数被当做短选项(我在这晕了一段时间), 如:
root@Ubt:~# getopt -l "help,verbose" -- --help --verbose
--verbose --
root@Ubt:~# getopt -o a: -l "help,verbose" -- --help --verbose
--help --verbose --

(7) 


阅读(1155) | 评论(0) | 转发(0) |
0

上一篇:sed_and_awk

下一篇:设计模式

给主人留下些什么吧!~~