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

Process substitution

~8 min · process-substitution, compare

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

command 출력을 파일로

<(cmd) 가 command 의 stdout 을 다음 프로그램이 읽을 수 있는 파일 경로로 만듦. diff 처럼 파이프 대신 파일명 받는 도구에 유용.

두 command 출력 비교

diff <(ls dir1) <(ls dir2)
diff <(sort a.txt) <(sort b.txt)
comm -23 <(sort users-now.txt) <(sort users-yesterday.txt)

임시 파일 불필요. Bash 가 뒤에서 /dev/fd/63 같은 FIFO 생성. consumer 는 진짜 파일처럼 읽음.

아웃바운드 — >(cmd)

some_cmd >(gzip > out.gz) 2> errors.log
tar c -f >(ssh remote 'cat > backup.tar') /home

덜 쓰지만 강력 — command 가 path 로 쓰고, 그 path 가 사실 다른 command 의 stdin.

그냥 파이프 쓰면 안 돼?

파이프는 입력 하나뿐. Process substitution 이 파일 인자 기대하는 도구에 둘 이상 입력 공급. diff / comm / paste / join 의 자연스런 모양.

POSIX sh 에선 안 됨

Process substitution 은 bash/zsh 확장. POSIX sh 스크립트는 같은 일에 임시 파일 (mktemp) 필요. 모던 스크립트는 sh 말고 bash 로 쓰는 또 하나의 이유.

Code

두 command 출력 비교·bash
# Compare two server's installed packages
diff <(ssh boxA dpkg -l | sort) <(ssh boxB dpkg -l | sort)
# Lines in left only
comm -23 <(sort a.txt) <(sort b.txt)
# Lines in right only
comm -13 <(sort a.txt) <(sort b.txt)

External links

Exercise

글로벌 zsh alias 를 다른 머신과 비교: diff <(ssh office alias) <(alias). 또는 sorted /etc/passwd 두 개. 임시 파일 흐름이 사라지는 거 체감.

Progress

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

댓글 0

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

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