xargs with multiple arguments

后端 未结 12 1741
独厮守ぢ
独厮守ぢ 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:57

    I was looking for a solution for this exact problem and came to the conclution of coding a script in the midle.

    to transform the standard output for the next example use the -n '\n' delimeter

    example:

     user@mybox:~$ echo "file1.txt file2.txt" | xargs -n1 ScriptInTheMiddle.sh
    
     inside the ScriptInTheMidle.sh:
     !#/bin/bash
     var1=`echo $1 | cut -d ' ' -f1 `
     var2=`echo $1 | cut -d ' ' -f2 `
     myprogram  "--file1="$var1 "--file2="$var2 
    

    For this solution to work you need to have a space between those arguments file1.txt and file2.txt, or whatever delimeter you choose, one more thing, inside the script make sure you check -f1 and -f2 as they mean "take the first word and take the second word" depending on the first delimeter's position found (delimeters could be ' ' ';' '.' whatever you wish between single quotes . Add as many parameters as you wish.

    Problem solved using xargs, cut , and some bash scripting.

    Cheers!

    if you wanna pass by I have some useful tips http://hongouru.blogspot.com

提交回复
热议问题