在makefile中的条件中使用shell命令的结果 [英] Use the result of a shell command in a conditional in a makefile

查看:486
本文介绍了在makefile中的条件中使用shell命令的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Makefile中的条件文件中执行命令.

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

我在外壳中工作了

if [ -z "$(ls -A mydir)" ]; then \
  echo "empty dir"; \
else \
  echo "non-empty dir"; \
fi

但是如果我在makefile中尝试,无论asdf是否为空,"$(ls -A mydir)"都将扩展为空:

but if I try it in a makefile, "$(ls -A mydir)" expands to nothing whatever if asdf is empty or not:

all:
    if [ -z "$(ls -A mydir)" ]; then \
      echo "empty dir"; \
    else \
      echo "non-empty dir"; \
    fi

ls命令没有按预期扩展:

The ls command does not expand as I expect:

$ mkdir mydir
$ make
if [ -z "" ]; then \
      echo "empty dir"; \
    else \
      echo "non-empty dir"; \
    fi
empty dir
$ touch mydir/myfile
$ make
if [ -z "" ]; then \
      echo "empty dir"; \
    else \
      echo "non-empty dir"; \
    fi
empty dir
$ ls -A mydir
myfile

如何使该命令在条件条件内工作?

How do I make the command work inside the conditional?

推荐答案

我在编写Makefile文件方面经验很少.但是我认为您必须在食谱中使用两个美元符号:

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/软件/make/manual/make.html#Variables-in-Recipes:

如果要在食谱中显示美元符号,则必须加倍 ("$$").

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

这是我更改您的makefile并添加$$(ls -A mydir)后的输出示例:

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

这篇关于在makefile中的条件中使用shell命令的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆