Makefile中的基本if else语句 [英] Basic if else statement in Makefile

查看:3666
本文介绍了Makefile中的基本if else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Makefile中执行一个简单的if else语句:

check:
  if [ -z "$(APP_NAME)" ]; then \
    echo "Empty" \
  else \
    echo "Not empty" \
  fi

当我执行make check时,出现以下错误:

if [ -z "" ]; then
/bin/bash: -c: line 1: syntax error: unexpected end of file
make: *** [check] Error 2

知道我在做什么错吗?

我知道我可以使用以下内容,但是在回声之后我有很多逻辑,因此我需要将其分布在多行中:

check:
  [ -z "$(PATH)" ] && echo "Empty" || echo "Not empty"

解决方案

将您的版本更改为此(添加分号):

check:
    if [ -z "$(APP_NAME)" ]; then \
        echo "Empty"; \
    else \
        echo "Not empty"; \
    fi

要在不带换行符的shell中评估语句(换行符被反斜杠\占用),您需要正确地以分号结尾.您不能在Makefile中使用真实的换行符作为条件shell脚本代码(请参见特定于背景的背景)

[ -z "$(APP_NAME)" ]echo "Empty"echo "Not empty"是需要评估的所有语句(类似于在键入命令后在终端中按Enter键).

具有特定背景的背景

make 会为一行中的每个命令生成一个新的shell,因此您不能像使用例行代码那样使用真正的多行shell代码.在脚本文件中.

最极端的是,这在shell脚本文件中是可能的,因为* newline **用作命令评估(就像在终端中按回车键是换行符一样):

if
[ 0 ]
then
echo "Foo"
fi

清单1

但是,如果您将其写入Makefile中,则会在其自己的外壳中对if进行评估(将外壳状态更改为 if ),然后从技术上对条件[ 0 ]进行评估再次使用自己的外壳,而与先前的if没有任何关系. 尽管make甚至都不会跳过第一个if,因为它希望退出代码继续到下一条语句,而仅将shell的状态更改为if不会获得该退出代码.

换句话说,如果make-target中的两个命令彼此完全独立(从来没有任何条件),则只需用普通的换行符将它们完美地分开,然后让它们在自己的shell中执行即可. /p>

因此,为了使 make 能够正确评估多行条件shell脚本,您需要在一行中评估整个shell脚本代码(因此所有代码都在同一个shell中进行评估).

因此,要在Makefile中正常工作,需要将清单1 中的代码转换为:

if \
[ 0 ]; \
then \
echo "Foo"; \
fi

最后一个命令fi不需要反斜杠,因为这是我们不再需要保持生成的外壳打开的地方.

I'm trying to execute a simple if else statement in a Makefile:

check:
  if [ -z "$(APP_NAME)" ]; then \
    echo "Empty" \
  else \
    echo "Not empty" \
  fi

When I execute make check I get the following error:

if [ -z "" ]; then
/bin/bash: -c: line 1: syntax error: unexpected end of file
make: *** [check] Error 2

Any idea what I'm doing wrong?

I know I can use the following, but I have a lot of logic after the echos so I need to spread it out across multiple lines:

check:
  [ -z "$(PATH)" ] && echo "Empty" || echo "Not empty"

解决方案

Change your version to this (adding semicolons):

check:
    if [ -z "$(APP_NAME)" ]; then \
        echo "Empty"; \
    else \
        echo "Not empty"; \
    fi

For evaluating a statement in shell without newline (newline gets eaten by the backslash \) you need to properly end it with a semicolon. You cannot use real newlines in a Makefile for conditional shell-script code (see Make-specific background)

[ -z "$(APP_NAME)" ], echo "Empty", echo "Not empty" are all statements that need to be evaluated (similar to pressing enter in terminal after you typed in a command).

Make-specific background

make spawns a new shell for each command on a line, so you cannot use a true multiline shell code as you would e.g. in a script-file.

Taking it to an extreme, this would be possible in a shell script file, because the *newline** acts as command-evaluation (like in the terminal hitting enter is a newline-feed):

if
[ 0 ]
then
echo "Foo"
fi

Listing 1

If you would write this in a Makefile though, if would be evaluated in its own shell (changing the shell-state to if) after which technically the condition [ 0 ] would be evaluated in its own shell again without any connection to the previous if. Although make will not even get past the first if because it expects an exit code to go on to the next statement, which it will not get from just changing the shell's state to if.

In other words if two commands in a make-target are completely independent of each other (no conditions what so ever) you could just perfectly fine seperate them only by a normal newline and let them execute each in its own shell.

So in order to make make to correctly evaluate multiline conditional shell scripts, you need to evaluate the whole shell-script-code in one line (so it all is evaluated in the same shell).

So for working correctly in a Makefile the code in Listing 1 needs to be translated to:

if \
[ 0 ]; \
then \
echo "Foo"; \
fi

The last command fi does not need the backslash because that's where we don't need to keep the spawned shell open anymore.

这篇关于Makefile中的基本if else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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