xargs with multiple arguments

后端 未结 12 1743
独厮守ぢ
独厮守ぢ 2020-12-13 08:05

I have a source input, input.txt

a.txt
b.txt
c.txt

I want to feed these input into a program as the following:

<         


        
12条回答
  •  执念已碎
    2020-12-13 08:56

    I stumbled on a similar problem and found a solution which I think is nicer and cleaner than those presented so far.

    The syntax for xargs that I have ended with would be (for your example):

    xargs -I X echo --file=X
    

    with a full command line being:

    my-program $(cat input.txt | xargs -I X echo --file=X)
    

    which will work as if

    my-program --file=a.txt --file=b.txt --file=c.txt
    

    was done (providing input.txt contains data from your example).


    Actually, in my case I needed to first find the files and also needed them sorted so my command line looks like this:

    my-program $(find base/path -name "some*pattern" -print0 | sort -z | xargs -0 -I X echo --files=X)
    

    Few details that might not be clear (they were not for me):

    • some*pattern must be quoted since otherwise shell would expand it before passing to find.
    • -print0, then -z and finally -0 use null-separation to ensure proper handling of files with spaces or other wired names.

    Note however that I didn't test it deeply yet. Though it seems to be working.

提交回复
热议问题