2008/05/27

Bash Process Substitution 命令代換

本文大致是翻譯自shell process redirection


為瞭解釋方便,先假設有 a, b, 兩個純文字檔,內容為:

$ cat a
e
d
c
b
a
$ cat b
g
f
e
d
c
b


一般命令的轉向可以結合多個命令成一個,是非常方便的,可是這觀念在早期是從 stdin, stdout 來看才正確,若有個命令要用到 file 的話,就很麻煩,例如 comm 是比對兩個檔案,針對上面的 a, b 要比對的話,可能要先 sort a | uniq > tmp_asort b | uniq > tmp_b,之後再用 comm -3 tmp_a tmp_b

底下直接列出比較方便的方式:

comm -3 <(sort a | uniq) <(sort b | uniq)

說明:
<(command list) 會把命令結果當輸入,所以上面 <(sort a | uniq) 結果會被當成一個 file 供 comm 當輸入用 >(command list) 會把命令結果寫入

0 意見: