As part of a (bash) script file, which has set -e, I do make distclean which, obviously, fails with the following error, if I have run distclean before calling the script:
make: *** No rule to make target 'distclean'. Stop
Is there way to do distclean and not fail, if nothing to clean?
CodePudding user response:
If you're okay with the error message being printed, and just want to ignore the failure and keep going, a common idiom is to append || :.
The general syntax here is cmd1 || cmd2. If cmd1 fails then it runs cmd2. : is the (unusual) name of a command that always succeeds, so || : has the effect of ignoring the first command's exit code.
make distclean || :
If you would rather not see an error message at all, you could check if the Makefile exists first:
if [[ -e Makefile ]]; then make distclean; fi
CodePudding user response:
You could do a
make distclean || echo "(Error from make ignored)"
to make it explicit what's happening.
