내가하려는 것은 파일에 ls 명령의 출력을 넣은 다음 grep 명령을 사용하여 해당 파일을 읽고 새 파일에 저장하고 그 내용을 기반으로합니다. 그 파일은 터미널에 무엇인가를 출력한다.dup 및 dup2 명령
그래서 다음 출력 리디렉션은도 : 표준 출력으로부터
1) 그렙 명령 (grep.txt하는 oioioi.txt에서 LS 명령)
2)에 대해 (oioioi.txt라고도 파일로)
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
//if(!fork())
//execl("/bin/grep","grep","input","oioioi.txt",0);
FILE *fp1,*fp2;
int fd,fd2;
fp1=fopen("oioioi.txt","w+");
fp2=fopen("grep.txt","w+");
fd=fileno(fp1);
fd2=fileno(fp2);
char filename[40],id[40],marks[40],filename2[40];
scanf("%s %s %s",filename,id,marks);
char *execarg2[]={"ls",0};
dup2(fd,1); //1st redirection
//close(1);
if(!fork())
{
execv("/bin/ls",execarg2);
}
sleep(1);
wait(NULL);
//dup(fd2);
close(fd);
dup(1);
dup2(fd2,1); //2nd redirection
if(!fork())
{
//sleep(1);
execl("/bin/grep","grep",filename,"oioioi.txt",0);
}
sleep(1);
wait(NULL);
close(fd2);
dup2(1,fd2); //3rd one which is the incorrect one
fscanf(fp2,"%s",filename2);
printf("%s",filename2);
if(strcmp(filename2,filename)==0)
printf("FILE FOUND");
return(0);
}
내가 그쪽으로 믿을 :
3) 여기
터미널
다시 grep.txt에서 내 코드입니다 3 번째가 틀립니다. 그러나 나는 처음 2 중 어느 것이 든 정말로 확신하지 못한다. 너희들이 내 코드를보고 내가 잘못 가고있는 곳을 알려주거나 다음 리디렉션을 위해 dup을 사용하는 시험을 주면 정말로 감사하겠다.
변수 이름이 (1) 대칭 (예 : fd1 = fileno (fp1); fd2 = fileno (fp2))이고 (2) 변수에 좀 더 의미있는 이름을 붙이면 도움이됩니다 –