일부 Unix 명령을 테스트하기위한 작은 c 프로그램을 만들고 있습니다. 나는 그가 테스트 할 수있는 선택권을 사용자에게 제공 한 다음 사용자가 선택을 할 수있게합니다. 사용자가 자신의 선택으로 2 번을 입력하면 파일에서 grep 명령을 테스트하는 다음 코드를 실행해야합니다. 하지만 "패턴"을 입력하면 코드 에 문제가 있습니다. 무한 루프가 시작됩니다. 도움이 필요하십니까? !! 나는 유닉스 프로그래밍에 많은 경험이 없다. 문제는 내 선택으로 숫자 2를 입력 할 때 나타납니다이 경우 2 번execl에서 grep을 사용하면 무한 루프가 시작됩니까?
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
char pattern[50];
int userInput;
pid_t childPID;
char filename[50];
FILE *file;
printf("Enter a File name:");
scanf("%s",filename);
file = fopen(filename, "r");
do{
printf("Enter what u want to do with the file:\n");
printf("1.Compress the file.\n");
printf("2.Search for a pattern at the file\n");
printf("3.Read the file (display the file content at the terminal)\n");
printf("4.Eject the program\n");
scanf("%d",&userInput);
switch(userInput){
case 1:
if(childPID == 0){
execl("/bin/gzip","gzip",filename,NULL);
exit(1);
}
break;
case 2: childPID = fork();
if(childPID ==0){
printf("Enter the pattern you want to search about:");
scanf("%s",pattern);
execl("/bin/grep","grep",pattern,filename,NULL);
}
break;
}
}while(userInput != 4);
return 0;
}
패턴 정의는 어떻게됩니까? 그것은 당신의 예제 패턴 문자열을 담을만큼 충분히 큽니까? –
당신은 옵션'-f'를 사용해야한다고 생각합니다.''-f "'와''filename'을 패스하는 것을 의미합니다. – Omkant
@OlafDietsche, 어떻게 패턴을 정의했는지입니다. char pattern [50]; –