Shell

如何將標準輸出通過管道傳輸到另一個程序?

  • July 25, 2014

我正在嘗試為我的程式碼設置一個 linter,我只想對目前分支中已更改的咖啡腳本文件進行 lint。因此,我使用以下命令生成文件列表git

git diff --name-only develop | grep coffee$

這給了我一個很好的我想要處理的文件列表,但我不記得如何將它傳遞給 linting 程序來實際完成工作。基本上我想要類似於find’s 的東西-exec

find . -name \*.coffee -exec ./node_modules/.bin/coffeelint '{}' \;

謝謝!

xargs是我正在尋找的 unix 實用程序。從手冊頁:

The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments.

Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from the standard input
 of xargs.  The utility is repeatedly executed until standard input is exhausted.

所以,我原來的問題的解決方案是:

git diff --diff-filter=M --name-only develop | grep coffee$ | xargs ./node_modules/.bin/coffeelint

只需通過一個while循環管道:

git diff --name-only develop | grep coffee$ | while IFS= read -r file; do
   ./node_modules/.bin/coffeelint "$file"
done

引用自:https://unix.stackexchange.com/questions/145956