Use the result of a shell command in a conditional in a makefile

前端 未结 1 696
孤城傲影
孤城傲影 2020-12-22 09:23

I am trying to execute a command in a conditional in a makefile.

I got it working in a shell:

if [ -z \"$(ls -A mydir)\" ]; then \\
  echo \"empty di         


        
相关标签:
1条回答
  • 2020-12-22 09:52

    I have little experience in writing makefiles. However I think you must use two dollar signs in your recipe:

    all:
        if [ -z "$$(ls -A mydir)" ]; then \
    

    https://www.gnu.org/software/make/manual/make.html#Variables-in-Recipes:

    if you want a dollar sign to appear in your recipe, you must double it (‘$$’).

    This is an example of output after I changed your makefile and added $$(ls -A mydir):

    $ ls mydir/
    1
    
    $ make
    if [ -z "$(ls -A mydir)" ]; then \
          echo "empty dir"; \
        else \
          echo "non-empty dir"; \
        fi
    non-empty dir
    
    $ rm mydir/1
    
    $ make
    if [ -z "$(ls -A mydir)" ]; then \
          echo "empty dir"; \
        else \
          echo "non-empty dir"; \
        fi
    empty dir
    
    0 讨论(0)
提交回复
热议问题