Christopher Smith
Christopher Smith

Reputation: 450

Use find to pass argument to program with parallel

I am trying to pass multiple arguments to a program.

find binaries/$package/lib -iname "*.txt" | parallel java -jar proguard/proguard.jar @obfuscateprofile -injars $package.jar -outjars binaries/$package/$pacakge.jar -libraryjars {}

I can use echo as a demonstration:

find binaries/derbypro/lib -iname "*.txt" | parallel echo -libraryjars {}

I want the program to get multiple -libraryjars arguments for each file found with find. For example, if I have a directory with the following files in it:

file1.txt
file2.txt
file3.txt

And I run the command, I want it to be equivalent to running the program like this:

programname -libraryjars file1.txt -libraryjars file2.txt -libraryjars file3.txt

But what I get instead is this:

programname -libraryjars file1.txt
programname -libraryjars file2.txt
programname -libraryjars file3.txt

So when I run

find binaries/derbypro/lib -iname "*.txt" | parallel echo -libraryjars {}

I get

-libraryjars file1.txt
-libraryjars file2.txt
-libraryjars file3.txt

So it is executing multiple echo programs. If I specify the number of parameters I am expecting with -N 3 it works fine, but I don't know how many files fine will find.

Upvotes: 0

Views: 80

Answers (3)

philippe lhardy
philippe lhardy

Reputation: 3286

you might ty this that uses bash function to decorate any result line from find with -libraryjars prefix :

find binaries/$package/lib -iname "*.txt" |  { while read LINE; do echo "-libraryjars $LINE"; done;} | xargs java -jar proguard/proguard.jar @obfuscateprofile -injars $package.jar -outjars binaries/$package/$package.jar

limitation is that it is can be fooled with files containing spaces or non orthodox characters ( seen as delimiters for xargs ).

Upvotes: 0

Vadym Savchuk
Vadym Savchuk

Reputation: 36

Try this:

programname -libraryjars `find binaries/$package/lib -name '*.txt' | xargs | sed 's/ / -libraryjars /g'`

I assume the number of .txt files is not huge.

Upvotes: 2

qqibrow
qqibrow

Reputation: 3022

parallel follows a map-reduce paradigm. it aims to separate large tasks into small ones and assign them to multiple programs, which doesn't satisfy your requirement.

For example, a simple map-reduce wc program using parallel would be:

cat XXX | parallel --block 10M --pipe wc -l | awk 'BEGIN{count=0;}{count = count+ $1;} END{print count;}'

Please check: How to write multicore sorting using GNU Parallel for more details.

In your case, what you want is programname -libraryjars file1.txt -libraryjars file2.txt -libraryjars file3.txt

you could concatenate your parameters like this:

find binaries/derbypro/lib -iname "*.txt" | awk '{params = params " " "-libraryjars " $1;} END {print params;}' | xargs programname

to run your program

Upvotes: 1

Related Questions