我是C语言的新手,正在尝试使用if语句来检查参数(在本例中为“jobs”),但它似乎没有用...
int builtin_cmd(char **argv)
{
printf("test1\n");
if (!strcmp(argv[0], "quit")) { //quit command
exit(0);
}
if ((!strcmp(argv[0], "fg")) || (!strcmp(argv[0], "bg"))) { //fg or bg command
do_bgfg(argv);
return 1;
}
if (!strcmp(argv[0], "jobs")) { //jobs command
printf("test2\n");
listjobs(jobs);
printf("test3\n");
return 1;
}
printf("test4\n");
return 0; /* not a builtin command */
}
我输入了“作业”,但是基于测试输出(1-4重复),它没有注册。有谁知道可能出什么问题了?
最佳答案
argv[0]
指向程序名称,而不是第一个参数。如果存在该参数,则argv[1]
指向该位置。
C标准,§ 5.1.2.2.1, Program Startup:
If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
强调我的。
关于c - C如果条件不接受参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54680580/