user1049697
user1049697

Reputation: 2509

How to control the placement of xargs parameters?

I have a Bash script that takes an input folder with source code and an output directory as arguments. I want to be able to compile the files in the source folder and then send them to an output folder. The problem is that xargs adds the read source files at the end of the argument, where I would like the output folder to be added instead. How can I control the placement of the xargs arguments? This is what I want to do: find $source -name '*.c' | xargs gcc <files> $output

Upvotes: 0

Views: 1526

Answers (3)

user1019830
user1019830

Reputation:

This task can also be solved using only Bash:

#!/bin/sh

if [ $# -ne 2 ]; then
    echo "usage: $0 [srcdir] [bindir]"
    exit 1
fi

src_dir="${1%/}"
bin_dir="${2%/}"

for src in "$src_dir"/*.c; do
    bin="$(basename "${src%.c}")"
    gcc "$src" -o "$bin_dir/$bin"
done

Upvotes: 0

Ole Tange
Ole Tange

Reputation: 33725

Use GNU Parallel:

find $source -name '*.c' | parallel gcc {} -o $output/{.}

Or if you want all the files in {}:

find $source -name '*.c' | parallel -Xj1 gcc {} -o $output

Learn more about GNU Parallel: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

10 second installation:

wget pi.dk/3 -qO - | sh -x

Upvotes: 1

imp25
imp25

Reputation: 2357

This can be achieved using:

find $source -name '*.c' | xargs gcc -o $output

This specifies the output of gcc before giving it the list of source files.

This problem cannot be solved by changing the location of the xargs argument, as if you do:

find $source -name '*.c' | xargs -I source gcc source -o $output

What will run, assuming you get source/a.c and source/b.c returned from find is:

gcc source/a.c -o $output
gcc source/b.c -o $output

Which I do not believe is the outcome you are after.

Upvotes: 2

Related Questions