help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Catching errors in pipes


From: Cook, Malcolm
Subject: RE: Catching errors in pipes
Date: Mon, 25 Mar 2024 05:45:47 +0000

>On Tue, 2023-12-05 at 13:43 +0100, Sébastien Hinderer wrote:
>> Is there a recommended way to catch errors in pipelines of coommands
>> that are used in recipes, please?
>
>This isn't a GNU Make issue. It's a shell programming issue.
>
>Make just invokes the shell and waits for it to exit, then looks at the
>exit code. If you want the behavior of pipefail you have to get your
>recipe to implement that behavior: there's nothing make can do about
>it.

Sébastien,

If you like to write your recipes using bash, you might try putting this at the 
top of your makefile:

## If .ONESHELL is mentioned as a target, then when a target is built
## all lines of the recipe will be given to a single invocation of the
## shell rather than each line being invoked separately (see Recipe
## Execution).  This is typically how we want to use make in a data
## process pipeline.
.ONESHELL:

### Use bash as shell for interpreting make commands instead of the
### default SHELL, which is '/bin/sh'.  Do this even though /bin/sh is
### often symlink to '/bin/bash' since bash behaves differently when
### invoked as 'sh'.  In particular, it does not perform 'Command
### Substitution' (or FIFO processing) for $(command).
SHELL=/usr/bin/bash

# -e: quit on 1st error ; -u: unset vars are an error ; pipefail: error in 
middle of pipeline is error for the pipeline
.SHELLFLAGS = -eu -o pipefail -c

Good luck,

Malcolm Cook

>
>I recommend checking on StackOverflow or similar places looking for
>idioms that will support this for POSIX shells.
>
>Just to note, starting with the next POSIX version, set -o pipefail is
>part of the standard so you can expect it to start appearing in POSIX-
>conforming shells that don't already support it. Of course, that
>doesn't help if you must support older shells.
>
>-- 
>Paul D. Smith <mailto:psmith@gnu.org> Find some GNU Make tips at:
>https://www.gnu.org http://make.mad-scientist.net
>"Please remain calm...I may be mad, but I am a professional." --Mad
>Scientist
>
>
>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]