使用 xargs 參數的內部函數調用

使用 xargs 參數的內部函數調用

我正在嘗試建立一個出現在我的/tmp目錄中的每個包含特定字串的檔案的檔案。

問題是調用basename {}似乎不起作用。這個也不是,也不是echo basename {}

grep -R 'mystring' . | cut -d: -f 1 | uniq | xargs -n 1 -I {} touch /tmp/`basename {}`

有誰知道如何在 xargs 參數上執行 basename 函數?

答案1

嘗試這個:

grep -R 'mystring' . | cut -d: -f 1 | uniq| xargs -n 1 -I {} -t -i ksh  -c "touch /tmp/$(basename {})"

答案2

問題是反引號擴展得太早(即發送到 xargs 的每個參數都不是一次)。可能有更好的方法,但您應該能夠擺脫顯式的 bash 循環:

grep -R 'mystring' . | cut -d: -f 1 | uniq | while read f; do touch "/tmp/`basename $f`"; done

相關內容