getopt()函数详细解释!保证看明白

研究select模型的时候看到CSDN很多博主用到了getopt函数,这个模型弄的一脸懵,getopt先弄明白。。

getopt() 方法是用来分析命令行参数的,它的作用是判断你输入的命令行是否正确。

1.命令行组成

我们举例说明命令行组成

ls -l -a /etc

这行命令的意思是:详细列出/etc这个文件夹下所有文件,包括隐藏文件的信息

ls ++命令 (command)++

a 和l 都是 ++选项 (option)++,选项前面,必须有短横线这才叫选项

/etc 这叫++operand ,操作对象++

grep -i water aaa.log

这行命令的意思是:在aaa.log文件中,查找water字符串,不区分大小写

grep 命令

i 选项

water ,正则表达式中叫匹配模式pattern ,命令行中叫做 ++选项参数 ( option argument)++

aaa.log 操作对象

比方说我瞎写 了一个aaa.log 其中内容是

water

water222

333water

444aaabbcc

555line

footabl

footballwater lunch

aaawatersuper

那么上书命令的 执行结果是

water
water222

333water

footballwater lunch

aaawatersuper

然后很多命令有不止一个参数,继续拿grep举例

grep -f aaa.log water aaa.log

这个命令意思是:在结果中每一行都打印一遍aaa.log 这个文件名,在文件aaa.log中查找water字符串,不区分大小写。然后执行结果是

aaa.log:water

aaa.log:water222

aaa.log:333water

aaa.log:444aaabbcc

aaa.log:555line

aaa.log:footabl

aaa.log:footballwater lunch

aaa.log:aaawatersuper

我晕死,我也不知道-f怎么不匹配的现在也在结果中变红了匹配了,但是这先不重要,弄明白命令行的组成就行了

2. getopt函数

2.1 函数原型

int getopt(int argc, char *const argv\[\], const char *optstring);

2.2 参数argc argv是什么意思

argc是你在命令行中的所有参数个数,argv\[\]是所有参数组成的数组,外加可执行文件名

cpp 复制代码
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char* argv[]){
    printf("参数个数是%d\n",argc);
    for(int i=0;i<argc;i++){
        printf("参数argv[%d] 是%s\n",i,argv[i]);
    }

return 0;
}

把这个代码gcc 编译成a.out

然后你 ./a.out hello world cat dog 回车,结果显示

参数个数是5

argv0 是 ./a.out

argv1 是 hello

argv2 是 world

argv3 是 cat

argv4 是 dog

2.3 。 这个函数有几个全局变量

getopt()函数有几个系统默认的全局变量,可以直接拿出来使用

  • optarg:当前选项的参数字符串 ,要么是null 要么是你写的,或者系统捕获的选项参数。
  • optind:下一个查找的位置,在所有命令行中的索引。
  • optopt:当发现无效选项字符时,optopt 就等于这个无效选项字符
  • opterr:1或者0 ,正常状态是1,表示打印错误
cpp 复制代码
//学习get_opt()
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char* argv[]){
    printf("参数个数是%d\n",argc);
    for(int i=0;i<argc;i++){
        printf("参数argv[%d] 是%s\n",i,argv[i]);
    }
    printf("test getopt func\n");
    int ret;
    while ((ret=getopt(argc, argv,"abc:")) !=-1){
	printf("返回值ret =%d ,int optind= %d,它的选项参数optarg =%s, 错误选项optopt =%c\n",ret,optind,optarg,optopt);
    }
return 0;
}

我的初衷是,只有-a -b -c 是合法选项, -c 后面必须接参数, abc 可以不全,但是 c后面必须有参数 。下面来实验下我瞎写的参数 或者故意写错会报什么

2.3.1 完全正确的输入

我输入 ./test2 -a -b -c wang

参数个数是5

参数argv0 是./test2

参数argv1 是-a

参数argv2 是-b

参数argv3 是-c

参数argv4 是wang

test getopt func

返回值ret =97 ,int optind= 2,它的选项参数optarg =(null), 错误选项optopt =

返回值ret =98 ,int optind= 3,它的选项参数optarg =(null), 错误选项optopt =

返回值ret =99 ,int optind= 5,它的选项参数optarg =wang, 错误选项optopt =

都对,所以到最后也没抓到哪个参数错误 (optopt一直是空的)

2.3.2 有一个不该出现的参数

./test2 -a -b -d wang

参数个数是5

参数argv0 是./test2

参数argv1 是-a

参数argv2 是-b

参数argv3 是-d

参数argv4 是wang

test getopt func

返回值ret =97 ,int optind= 2,它的选项参数optarg =(null), 错误选项optopt =

返回值ret =98 ,int optind= 3,它的选项参数optarg =(null), 错误选项optopt =

./test2: invalid option -- 'd'

返回值ret =63 ,int optind= 4,它的选项参数optarg =(null), 错误选项optopt =d

2.3.3 打乱参数顺序

./test2 -b -c -d wang

参数个数是5

参数argv0 是./test2

参数argv1 是-b

参数argv2 是-c

参数argv3 是-d

参数argv4 是wang

test getopt func

返回值ret =98 ,int optind= 2,它的选项参数optarg =(null), 错误选项optopt =

返回值ret =99 ,int optind= 4,它的选项参数optarg =-d, 错误选项optopt =

没抓到错误,因为 选项c 后面的-d被认为是选项参数了,后面的wang酒忽略不看了。

未完待续额,后面写getopt _long

相关推荐
zylyehuo14 小时前
Linux 彻底且安全地删除文件
linux
用户805533698031 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297911 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux
乘云数字DATABUFF1 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
Web3探索者3 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo3 天前
Linux系统中网线与USB网络共享冲突
linux
荣--3 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森3 天前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜4 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
Sokach10154 天前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux