2014-09-27 2 views
0

운영 체제가 새로 도입되었으며 아래 언급 된 다음 명령을 실행하려고하지만 작동하지 않는 이유를 해결할 수 없습니다. ls -l | grep D 실행 중 오류 발생 D | grep De

나는 명령

ls -l | grep D|grep De 

을 실행하려고이 내 코드입니다 -

#include<stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 

int main() 
{ 
    int fd[2]; 
    int fd2[2]; 
    pipe(fd); 
    if(!fork()) 
    { 
     pipe(fd2); 
     if(!fork()) 
     { 
      close(0); 
      dup(fd[0]); 
      close(1); 
      close(fd[1]); 
      dup2(fd2[1],fd[0]); 
      close(fd[0]); 
      execlp("grep","grep","D",NULL); 
     } 
     else 
     { 
      close(fd[0]); 
      dup(fd2[0]); 
      close(fd[1]); 
      execlp("grep","grep","De",NULL); 
     } 
    } 

    else 
    { 
     close(1); 
     dup(fd[1]); 
     close(0); 
     execlp("ls","ls","-l",NULL); 
    } 
    return 0; 
} 

이 명령을 실행하도록 도와주세요.

#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    char command[50]; 

    strcpy(command, "ls -l | grep D|grep De"); 
    system(command); 

    return(0); 
} 

system 명령은 호스트 환경에 명령에 의해 지정된 명령 이름 또는 프로그램 이름을 통과 실행되는 : 여기

답변

0

이 C 코드에서 해당 명령을 실행하는 쉬운 방법입니다 사전에 U 감사 명령 프로세서에 의해 명령이 완료된 후 리턴합니다. 쉘 스크립트를 포함 SHELLSCRIPT :

#include <stdio.h> 
#include <stdlib.h> 

#define SHELLSCRIPT "\ 
ls -l | grep D|grep De" 

int main() 
{ 
puts("Will execute sh with the following script :"); 
puts(SHELLSCRIPT); 
puts("Starting now:"); 
system(SHELLSCRIPT); 
return 0; 
} 

#define SHELLSCRIPT 지시어는 명명 된 상수를 정의하는 C에 사용됩니다 : 당신이 명령은 미래에 너무 복잡 얻을 경우 여기

쉘 스크립트를 실행하는 또 다른 방법입니다.

각 행 끝의 백 슬래시 \은 가독성을 높이기 위해 다음 행에 코드를 입력하는 데 사용됩니다.

질문이 있으시면 알려주세요.