C.W.K.
Stream
Lesson 05 of 10 · published

Pipes — Unix 의 초능력

~12 min · pipe, composition, philosophy

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

컴퓨팅을 바꾼 글자

파이프 | 가 의미하는 건 — '왼쪽 command 의 stdout 을 오른쪽 command 의 stdin 에 연결.' 작은 프로그램 두 개가 타이핑 속도로 더 큰 프로그램이 돼. 1973 년 Doug McIlroy 가 도입했을 때 Bell Labs 가 박수쳤다고 함 — Unix 가 그 순간 막을 수 없는 도구가 됐어.

멘탈 모델

파이프 단계 각각이 프로세스. 병렬로 돌고 kernel 이 사이를 버퍼링. cmd1 | cmd2 | cmd3 는 프로세스 셋 띄움. 각자 읽을 수 있는 만큼 읽고 생산해. 중간 파일 없음 — 다 in-memory 스트림.

왼쪽부터 오른쪽으로

파이프 = 순서 있는 레시피: 'A 해, 결과 B 에 줘, 결과 C 에 줘.' ps aux | grep python | grep -v grep | wc -l 은 '모든 프로세스 나열, python 만 남김, grep 자기 자신 제거, 줄 수 세기'. 영어 / 한국어를 파이프 형태로 쓸 수 있으면 모델 체화 끝.

Pipe exit code

기본 파이프라인의 exit code = 마지막 command 의 거. cat missing | wc -l 은 wc 성공이라 0. set -o pipefail 면 어느 단계든 실패 시 전체 실패. 스크립트엔 항상 켜기 (scripting 트랙에서 set -euo pipefail 풀세트 다룸).

파이프가 안 맞는 경우

파이프는 byte 단위 스트리밍. sort, 특정 jq 필터, random access 가 필요한 도구는 입력 전부를 버퍼링 후 출력. 보통은 OK 지만 GB 단위 스트림은 named pipe (FIFO) 또는 병렬 분기는 process substitution 으로.

Code

전형적인 파이프 패턴·bash
# Top 10 most common shell commands
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
# All running python processes (excluding grep)
ps aux | grep python | grep -v grep
# Word count for a project
find . -name '*.py' | xargs wc -l | tail -1
pipefail 이 살린다·bash
set -o pipefail
cat missing.txt | wc -l   # exits 1 (cat failed) instead of 0
echo "$?"

External links

Exercise

history | awk '{print $2}' | sort | uniq -c | sort -rn | head 으로 가장 많이 쓴 command 확인. ps aux | wc -l 로 프로세스 개수. set -o pipefail; false | true; echo $? — 0 대신 1 나오는 거 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.