Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1427724
  • 博文数量: 842
  • 博客积分: 12411
  • 博客等级: 上将
  • 技术积分: 5772
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-14 14:43
文章分类

全部博文(842)

文章存档

2013年(157)

2012年(685)

分类: LINUX

2012-03-18 00:49:02

ShellgetoptsC语言的getopt对比

 

Note:shellgetopts来处理shell脚本命令行参数的的,c语言的getopt函数是处理C程序命令行参数的,

 

注意:在shellcOPTINDoptind都指向下一个参数的位置

1、 Shellgetopts

#! /bin/bash -

 

while getopts f:v opt

do

    echo $OPTIND

    case $opt in

    f)  echo $OPTARG

        echo $OPTIND;;

    v)

        echo 'no v'

echo $OPTARG;;

    ?)      echo 'error'

            echo $OPTARG

    esac

done

echo $OPTIND

 

$./test –f

./test 选项需要一个参数 –f #getopts f:v情形下系统报错,在getopts :f:v系统不报

2

error #-f本来应该有选项,但是没有指定,则将opt变量设置成’?’,但是并不把OPTARG

#置成’f’,而是空值NULL

2

 

$/test –f –v

3

-v#-f有选项,但是不应该是-v,但是shell-v作为了-f的选项变量即是OPTARG=’-v’

3

3

 

$/test –v argvother

2

no v#-v没有选项,OPTARG是空值NULL,即使-v后面有内容也不识别为getopts处理的参数

    #跳出了getopts的循环

2

 

#! /bin/bash -

 

while getopts :f:v opt

do

    echo $OPTIND

    case $opt in

    f)  echo $OPTARG

        echo $OPTIND;;

    v)

        echo 'no v'

echo $OPTARG;;

    ?)      echo 'error'

            echo $OPTARG

    esac

done

echo $OPTIND

$./test –f#getopts f:v情形下系统报错,在getopts :f:v系统不报错

2

error #-f本来应该有选项,但是没有指定,则将opt变量设置成’?’,把OPTARG

f     #置成’f’

2

 

 

2、 C语言的getopt

#include

#include

int main(int argc,char *argv[])

{

  int ch;

  while((ch=getopt(argc,argv,"f:v"))!=-1)

  {

    printf("optind:%d ",optind);

    printf("optarg:%s ",optarg);

    printf("ch:%c ",ch);

    switch(ch)

    {

      case 'f':

        printf("option f ");

        break;

      case 'v':

        printf("option v ");

        break;

      case '?':

        printf("option ? ");

break;

 

case ':':

        printf("option : ");

        break;

 

    }

    printf("\n");

  }

}   

 

./test –f

./a.out: option requires an argument -- 'f'

optind:2 optarg:(null) ch:? option ?

#-f本来应该有选项,但是没有指定,则将#opt变量设置成’?’,但是并不把OPTARG置成’f’,而是空值NULL

./test –f -v

optind:3 optarg:-v ch:f option f

./test –v otherargv

optind:2 optarg:(null) ch:v option v

#-v没有选项,参数为空

 

 

#include

#include

int main(int argc,char *argv[])

{

  int ch;

  while((ch=getopt(argc,argv,":f:v"))!=-1)

  {

    printf("optind:%d ",optind);

    printf("optarg:%s ",optarg);

    printf("ch:%c ",ch);

    switch(ch)

    {

      case 'f':

        printf("option f ");

        break;

      case 'v':

        printf("option v ");

        break;

      case '?':

        printf("option ? ");

        break;

case ':':

        printf("option : ");

        break;

 

    }

    printf("\n");

  }

}   

./test –f

optind:2 optarg:(null) ch:: option :

#-f本来应该有选项,但是没有指定,则将#opt变量设置成’:’,但是并不把OPTARG置成’f’,而是空值NULL

 

 

总结:-f后需有参数,-v后无参数

加上前缀’:’限制系统自动输出错误信息;

ShellgetoptsCgetopt只在处理错误的时候不一致: -f选项后无参数

(1)  shellgetopts在不加前缀冒号(f:v)的时候,opt=?OPTARG=NULL;报错

(2)  shellgetopts在加前缀冒号(:f:v)的时候,opt=?OPTARG=f;不报错

(3)  cgetopt在不加前缀冒号(f:v)的时候,opt=?OPTARG=NULL;报错

(4)  cgetopt在加前缀冒号(:f:v)的时候,opt=:OPTARG=NULL;不报错

 

注:暂存的内容只能恢复到当前文章的编辑器中,如需恢复到其他文章中,请编辑该文章并从暂存箱中恢复;或者直接复制以上内容,手工恢复到相关文章。

阅读(497) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~