I'd like to conditionally execute a command in a gnu makefile, based on the return value of another command.
To be specific, something like this:
lsmod | grep -q pfc && sudo rmmod pfc
If the current list of modules as output by lsmod
contains pfc
, remove it. This works, with the small problem that if the grep
command fails (module not present) the whole pipeline returns non-zero, which causes the command to be interpreted as failed, but in fact this is a success.
I could just add a ; true
at the end to always force it to success, but this won't catch failures in the rmmod
command!
I'd like something that mostly portably across sh
implementations, but technically I guess I'm using dash
since that's where sh
points on Ubuntu.
You could just handle the expected failure by cleaning the exit value with a nop command like
echo -n
:Should the output be missing the nop command is executed and the whole line returns with
$?=0
.Edit:
The simpler nop's that were suggested would look like:
resp