Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The command $ make all gives errors such as rm: cannot remove '.lambda': No such file or directory so it stops. I want it to ignore the rm-not-found-errors. How can I force-make?

Makefile

make clean make .lambda make .lambda_t make .activity make .activity_t_lambda clean: rm .lambda .lambda_t .activity .activity_t_lambda .lambda: awk '{printf "%.4f \n", log(2)/log(2.71828183)/$$1}' t_year > .lambda .lambda_t: paste .lambda t_year > .lambda_t .activity: awk '{printf "%.4f \n", $$1*2.71828183^(-$$1*$$2)}' .lambda_t > .activity .activity_t_lambda: paste .activity t_year .lambda | sed -e 's@\t@\t\&\t@g' -e 's@$$@\t\\\\@g' | tee > .activity_t_lambda > ../RESULTS/currentActivity.tex

Try the -i flag (or --ignore-errors ). The documentation seems to suggest a more robust way to achieve this, by the way:

To ignore errors in a command line, write a - at the beginning of the line's text (after the initial tab). The - is discarded before the command is passed to the shell for execution.

For example,

clean:
  -rm -f *.o
  

This causes rm to continue even if it is unable to remove a file.

All examples are with rm, but are applicable to any other command you need to ignore errors from (i.e. mkdir).

Don't do that! You shouldn't ignore errors. Just add the -f flag to rm and it'll no longer fail when trying to delete files which don't exist. It will however still return and error if it really fails to delete a file. That's the behaviour you want, fail when there's a problem! – Kristof Provost Mar 27, 2012 at 6:21 @Kristof Provost Agreed. rm -f is better for the specific problem the user is having, but it's still nice to know about the general solution even if it's sometimes unsafe. – brian_o Sep 4, 2015 at 16:07 Just noting there are cases where it's legitimate to ignore the error code: for example, a make rule which for informative purposes shows a diff against a previous version. Standard diff(1) returns 1 for files which differ, so you might want an action like -diff -N [email protected] $@. However, as this will still give you a warning (gnu make), you might prefer diff -N [email protected] $@ || true, which is quieter, though does look ugly. – jonathanjo May 24 at 8:43 I didn't want to say anything, but I was wondering what their thinking was too. If there's some reason I don't see why that flag would not be appropriate, it would be a good thing to bring up. – T.E.D. Apr 20, 2010 at 12:41 You shouldn't ignore errors. The solution proposed by Brian, Oded and NebuSoft are correct. This one and the accepted answer are wrong. – Kristof Provost Mar 27, 2012 at 6:22 @KristofProvost - Ah. Fair enough I guess. Generally I also believe answers that get at the root of the problem are superior to ones (like this one) that meerly answer the asked question. I'm not sure I'd downvote somebody for that, but different strokes... – T.E.D. Jul 25, 2012 at 16:26 Nice answer. It contains a straightforward, top-level flag which no other answer contains, yet still recommends the correct behavior. Errors shouldn't be ignored, but it's good to know the options. – brian_o Sep 4, 2015 at 16:14 Something useful to do is to ignore compiler errors... which allows make to build as many compilation units as possible while you go fix whatever the compiler choked on. That way when you've fixed whatever was broken, you don't have to wait for everything else to build too. – inetknght Feb 1, 2016 at 16:35

Return successfully by blocking rm's returncode behind a pipe with the true command, which always returns 0 (success)

rm file || true
                You can also use rm file || true.   That way true is called if rm returns an error code.  But any console output from rm might produce is not swallowed by the pipe.
– Jason McVetta
                Aug 31, 2020 at 14:14
                I would even say that you should use ` || true` when your real intent is not to pipe stdout.
– Mikko Rantalainen
                Jan 30, 2021 at 11:57
                I imagine this was a typo all along. Since this is a canonical for this question and this answer is highly upvoted, I fixed the typo to recommend the correct and idiomatic usage over the confused and accidental (but in fact coincidentally working) original code.
– tripleee
                Aug 7, 2021 at 19:16

To get make to actually ignore errors on a single line, you can simply suffix it with ; true, setting the return value to 0. For example:

rm .lambda .lambda_t .activity .activity_t_lambda 2>/dev/null; true

This will redirect stderr output to null, and follow the command with true (which always returns 0, causing make to believe the command succeeded regardless of what actually happened), allowing program flow to continue.

This works for me where the leading dash does not (I'm given a makefile to run a test which needs to fail, and will parse the logs later) – Sean Houlihane Sep 6, 2017 at 9:11

Usage of - seems to be a proper way of handling such situations as it can ignore the error of a specific command, but not all errors.

You can find more information by the following link.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.