Xargs

如何使用 xargs 處理空格分隔的環境變數

  • August 2, 2021

我正在嘗試並行執行一個長時間執行的程序。程序每次執行的參數儲存在一個以空格分隔的環境變數中。這是我試圖執行的一個人為的例子:

$ echo -e 1 2 3 4 | xargs --max-procs=3 --max-args=1 --replace=% echo % is the number being processed

這是該命令的輸出:

1 2 3 4 is the number being processed

為什麼 max-args 似乎被忽略了?然後我嘗試顯式設置分隔符以提供更好的結果:

$ echo -e 1 2 3 4 | xargs -d " " --max-procs=3 --max-args=1 --replace=% echo % is the number being processed
1 is the number being processed
2 is the number being processed
3 is the number being processed
4
is the number being processed

xargs 在處理第四個參數時在做什麼?

經過一番搜尋,我確實設法幾乎得到了我想要的東西。參數已正確處理,但並行性不起作用(使用此處未顯示的另一個命令進行驗證):

$ echo -e 1 2 3 4 | xargs -n 1 | xargs --max-procs=3 --max-args=1 --replace=% echo % is the number being processed
1 is the number being processed
2 is the number being processed
3 is the number being processed
4 is the number being processed

我錯過了什麼?

echo -e 1 2 3 4 | sed -e 's/\s\+/\n/g' | xargs --max-procs=3 --max-args=1 --replace=% echo % is the number being processed

完成任務?輸出似乎是正確的:

1 is the number being processed
2 is the number being processed
3 is the number being processed
4 is the number being processed

我還嘗試替換echosleep以確認它並行執行,它確實:

echo -e 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 | sed -e 's/\s\+/\n/g' | xargs --max-procs=20 --max-args=1 --replace=% sleep 1

引用自:https://serverfault.com/questions/516602