4

I'm running a command to find all make files in a directory and run make build.

I would like to get xargs to stop on the first make build that fails.

Here's the command I have so far.

$ find . -name "Makefile" | xargs dirname | xargs -I {} make -C {} build

The problem is that xargs keeps going even after a make build fails. When I check the final status code, here's what I see.

$ echo $?
123

And when I checked the man page. Here's what it says.

123 if any invocation of the command exited with status 1-125

Which makes sense because some of those make builds failed with exit 1 or something.

Is there a way to get xargs to stop on the first make build error?

1 Answer 1

7

XARGS(1):

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input.

It would be possible to use a subshell to return 255:

find . -name "Makefile" | xargs dirname | \
  xargs -I {} sh -c 'make -C "$1" build || exit 255' sh {}
1
  • Aaah, okay. I was thinking there would be some extra flag or something as part of xargs itself. This works too though!
    – 425nesp
    Commented Mar 5, 2020 at 5:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .