Filters
- 입력(standard input)을 받아서, 변형을 가한 후, 출력(standard output)으로 내보내는 명령어들을 filter라고 부른다.
- head, tail, more, sort, grep, awk..
- 모든 매개변수들은 command-line arguments를 통해 주어진다.
- “cat” 명령어는 가장 간단한 버전의 filter이다. 왜 그러냐면 입력을 받아서 번형을 하지 않은 후 출력으로 내보내기 때문이다. 여기서 변형을 다르게 가하면 다른 명령어가 된다.
Redirection
- 프로그램은 file descriptor table의 entry를 가지고 있다.
- 이 entry는 특정한 system file table에 연결되어 있다.
- redirection을 이용하여 연결되어 있는 system file table을 바꿀 수 있다.
- > : 왼쪽의 출력을 오른쪽에 입력한다.
- < : 오른쪽의 출력을 왼쪽에 입력한다.
Redirection in C program
#include<unistd.h>
• int dup2(int fildes, int fildes2);
dup2()는 file descriptor값을 복사해주는 역할을 하는 함수이다. 만약 파일이 open되어 있으면 먼저 close한다. 그 후 filedes를 filedes2로 복사한다.
- int fildes : 복사할 file descriptor값이다.
- int fildes2 : 복사한 값을 받을 file descriptor값이다.