How do I check if file exists in Makefile so I can delete it?

前端 未结 12 2164
心在旅途
心在旅途 2020-12-02 07:40

In the clean section of my Makefile I am trying to check if the file exists before deleting permanently. I use this code but I receive errors.

What\'s w

12条回答
  •  孤城傲影
    2020-12-02 07:49

    The problem is when you split your command over multiple lines. So, you can either use the \ at the end of lines for continuation as above or you can get everything on one line with the && operator in bash.

    Then you can use a test command to test if the file does exist, e.g.:

    test -f myApp && echo File does exist
    

    -f file True if file exists and is a regular file.

    -s file True if file exists and has a size greater than zero.

    or does not:

    test -f myApp || echo File does not exist
    test ! -f myApp && echo File does not exist
    

    The test is equivalent to [ command.

    [ -f myApp ] && rm myApp   # remove myApp if it exists
    

    and it would work as in your original example.

    See: help [ or help test for further syntax.

提交回复
热议问题