201

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

all:
        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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
hhh
  • 50,788
  • 62
  • 179
  • 282

8 Answers8

377

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).

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 56
    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 '12 at 06:21
  • 27
    @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 04 '15 at 16:07
  • 6
    Make also provides `$(RM)`, which you can use instead of `rm -f`. – reitermarkus Feb 18 '19 at 11:34
  • 2
    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 $@.prev $@`. However, as this will still give you a warning (gnu make), you might prefer `diff -N $@.prev $@ || true`, which is quieter, though does look ugly. – jonathanjo May 24 '23 at 08:43
65

make -k (or --keep-going on gnumake) will do what you are asking for, I think.

You really ought to find the del or rm line that is failing and add a -f to it to keep that error from happening to others though.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 1
    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 '10 at 12:41
  • 3
    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 '12 at 06:22
  • 1
    @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 '12 at 16:26
  • 6
    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 04 '15 at 16:14
  • 4
    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 01 '16 at 16:35
  • 1
    @KristofProvost Another valid reason for wanting this is when you use make to run tests or deploy or other automation. Make is not *just* for compiling. So knowing the options is fine. – berkes Dec 25 '21 at 09:54
60

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

rm file || true
tripleee
  • 175,061
  • 34
  • 275
  • 318
Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36
  • 16
    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 '20 at 14:14
  • 5
    I would even say that you *should* use ` || true` when your *real intent* is not to pipe `stdout`. – Mikko Rantalainen Jan 30 '21 at 11:57
  • 1
    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 07 '21 at 19:16
  • I prefer `find ... -delete` if `rm -f` is a bad idea – CervEd May 04 '23 at 10:25
23

Change clean to

rm -f .lambda .lambda_t .activity .activity_t_lambda

I.e. don't prompt for remove; don't complain if file doesn't exist.

Brian Carlton
  • 7,545
  • 5
  • 38
  • 47
19

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.

Riot
  • 15,723
  • 4
  • 60
  • 67
  • 1
    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 06 '17 at 09:11
3

Put an -f option in your rm command.

rm -f .lambda .lambda_t .activity .activity_t_lambda
nalply
  • 26,770
  • 15
  • 78
  • 101
NebuSoft
  • 3,962
  • 2
  • 22
  • 24
2

Change your clean so rm will not complain:

clean:
    rm -f .lambda .lambda_t .activity .activity_t_lambda
Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

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.