ls -a -l
要读入命令行,就读入-,后面的a和l是体现性质的作用
这个过程用C语言实现是困难的,需要用脚本语言
1.希望实现的功能如下:
流程图:
2. 用到的知识点:
man 3 getopt
查看getopt这个函数的作用
#include <stdio.h>
#include <stdlib.h>// 用到了exit(0);
#include <unistd.h>//用到了getopt函数
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "la")) != -1) {
switch (opt) {
case 'l':
l_flag = 1;
break;
case 'a':
a_flag = 1;
break;
default:
fprintf(stderr, "Usage : %s [-la]!\n", argv[0]);
exit(1);
}
}
if (l_flag == 1) printf(“has l");
if (a_flag == 1) printf("has a");
return 0;
}
现在我们就简单实现了命令行解析,看到了我加进来的参数有哪些。用到while((opt = getopt(argc, argv, "la")) != -1)
这是一个流式读取,这时候
opt = getopt(argc, argv, “la”), 读到了opt == -1的时候就不能继续读啦。这时候我们看opt 这个参数是什么