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

前端 未结 12 2053
心在旅途
心在旅途 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.

    0 讨论(0)
  • 2020-12-02 07:52

    The answers like the one from @mark-wilkins using \ to continue lines and ; to terminate them in the shell or like the ones from @kenorb changing this to one line are good and will fix this problem.

    there's a simpler answer to the original problem (as @alexey-polonsky pointed out). Use the -f flag to rm so that it won't trigger an error

    rm -f myApp
    

    this is simpler, faster and more reliable. Just be careful not to end up with a slash and an empty variable

    rm -f /$(myAppPath) #NEVER DO THIS

    you might end up deleting your system.

    0 讨论(0)
  • 2020-12-02 07:54

    Missing a semicolon

    if [ -a myApp ];
    then
      rm myApp
    fi
    

    However, I assume you are checking for existence before deletion to prevent an error message. If so, you can just use rm -f myApp which "forces" delete, i.e. doesn't error out if the file didn't exist.

    0 讨论(0)
  • 2020-12-02 07:54
    ifneq ("$(wildcard $(PATH_TO_FILE))","")
        FILE_EXISTS = 1
    else
        FILE_EXISTS = 0
    endif
    

    This solution posted above works best. But make sure that you do not stringify the PATH_TO_FILE assignment E.g.,

    PATH_TO_FILE = "/usr/local/lib/libhl++.a" # WILL NOT WORK
    

    It must be

    PATH_TO_FILE = /usr/local/lib/libhl++.a
    
    0 讨论(0)
  • 2020-12-02 07:55

    It may need a backslash on the end of the line for continuation (although perhaps that depends on the version of make):

    if [ -a myApp ] ; \
    then \
         rm myApp ; \
    fi;
    
    0 讨论(0)
  • 2020-12-02 07:55
    test ! -f README.md || echo 'Support OpenSource!' >> README.md
    

    "If README.md does not exist, do nothing (and exit successfully). Otherwise, append text to the end."

    If you use && instead of || then you generate an error when the file doesn't exist:

    Makefile:42: recipe for target 'dostuff' failed
    make: *** [dostuff] Error 1
    
    0 讨论(0)
提交回复
热议问题