【发布时间】:2018-07-01 22:43:22
【问题描述】:
我正在尝试运行以下代码,但它不起作用。当程序的输入名称完成后,我只想执行一个程序。我不知道问题出在哪里,因为代码看起来还可以。也许还有其他重要的事情,我没有注意到......
setbuf(stdout, NULL);
char input[255];
char path[255];
int status;
char *args[2] = {"ls", NULL};
while(strcmp(input, "end") != 0 ){
printf("Waiting for input:\n");
scanf("%s",input);
strcpy(path, "./");
strcat(path, input);
if(strcmp(input, "end") != 0){
printf("execute %s\n", path);
int ret = execv(path, args);
if(ret == -1){
perror("execve error");
}
}
else{
printf("Programm-Ends\n");
}
};
return 0;
【问题讨论】:
-
a) 制作minimal reproducible example b) 详细说明“不起作用” c) 描述调试尝试的结果 d) 作为幸运符,获得参加tour 的徽章跨度>
-
调试提示:a) scanf() 的返回值是什么 b) 该值是什么意思 d) ericlippert.com/2014/03/05/how-to-debug-small-programs e) stackoverflow.com/questions/2069367/how-to-debug-using-gdb f) ericlippert.com/2014/03/21/find-a-simpler-problem
-
避免被否决的提示:a) 删除问题 b) 编辑问题(参见我的其他 cmets)c) 当您有更多信息和改进的问题时,取消删除它
-
你的负分问题迟早会给你带来麻烦 (stackoverflow.com/help/question-bans)。接受建议清理它们。
-
args[0]通常应该是程序的名称。您不应该将其硬编码为ls。
标签: c shell ubuntu process execution