Makefile中如何给变量赋值?
How to assign value to variable in Makefile?
在尝试测试 Makefile 中的命令失败时(在本例中为 jekyll build relative
),变量 $$?
应包含最后一个命令的退出代码。在测试中,此代码显示为 127。但是,我想将其分配给变量 LAST_EXIT
,以便下一个 if 语句可以检查执行的命令是成功还是失败。
问题是 LAST_EXIT
从未收到分配给它的值,如下面的代码所示。关于如何解决这个问题有什么建议吗?
代码:
LAST_EXIT = 0
all:
@echo "Building the website... Running command:"
- jekyll build relative || LAST_EXIT=$$?
- jekyll build relative || echo $$? and $(LAST_EXIT)
ifeq ($(LAST_EXIT), 0)
#echo a message indicating success
输出:
jekyll build relative || LAST_EXIT=$?
/bin/sh: jekyll: command not found
jekyll build relative || echo $? and 0
/bin/sh: jekyll: command not found
127 and 0
你的方法有两个问题。
1) Make 在执行任何规则之前解析 makefile 的条件部分。这个:
all:
LAST_EXIT=0
ifeq ($(LAST_EXIT), 0)
#echo a message indicating success
endif
不会报告成功(除非您将 LAST_EXIT
的值设置在规则之上的某处)。
2) 配方中的每个命令都在其自己的子程序中执行shell; shell 变量值不 从一行到下一行保留:
all:
LAST_EXIT=5; echo the value is $$LAST_EXIT
@echo now the value is $$LAST_EXIT
这应该有效:
all:
- jekyll build relative || LAST_EXIT=$$?; \
if [ $$LAST_EXIT == 0 ]; then echo success!; fi
我对这个问题的解决方案是在 Makefile 中调用以下脚本来检查 Jekyll:
(称为 ./{scriptName
}
#!/bin/bash
LINEBREAK="*****************************************************************"
VERSION=0
echo "Checking Jekyll..."
VERSION=$(jekyll --version)
if test "$?" == "0"
then
echo "$VERSION is installed on this system..."
else
echo "$LINEBREAK"
echo "Oops! It looks like you don't have Jekyll yet, lets install it!"
echo "Running command: \"sudo gem install jekyll\""
echo "$LINEBREAK"
sudo gem install jekyll
if test "$?" != "0"
then
echo "$LINEBREAK"
echo "Jekyll install failed... It needs to be installed as a super-user on your system, which"
echo "requires your password. You can run \"sudo gem install jekyll\" yourself to install Jekyll."
echo "You can also see their website at: \"https://jekyllrb.com/docs/installation/\" for more information"
echo "$LINEBREAK"
exit 113
else
echo "$LINEBREAK"
echo "Jekyll has been installed on this system..."
echo "Proceeding to build..."
echo "$LINEBREAK"
fi
fi
exit 0
在尝试测试 Makefile 中的命令失败时(在本例中为 jekyll build relative
),变量 $$?
应包含最后一个命令的退出代码。在测试中,此代码显示为 127。但是,我想将其分配给变量 LAST_EXIT
,以便下一个 if 语句可以检查执行的命令是成功还是失败。
问题是 LAST_EXIT
从未收到分配给它的值,如下面的代码所示。关于如何解决这个问题有什么建议吗?
代码:
LAST_EXIT = 0
all:
@echo "Building the website... Running command:"
- jekyll build relative || LAST_EXIT=$$?
- jekyll build relative || echo $$? and $(LAST_EXIT)
ifeq ($(LAST_EXIT), 0)
#echo a message indicating success
输出:
jekyll build relative || LAST_EXIT=$?
/bin/sh: jekyll: command not found
jekyll build relative || echo $? and 0
/bin/sh: jekyll: command not found
127 and 0
你的方法有两个问题。
1) Make 在执行任何规则之前解析 makefile 的条件部分。这个:
all:
LAST_EXIT=0
ifeq ($(LAST_EXIT), 0)
#echo a message indicating success
endif
不会报告成功(除非您将 LAST_EXIT
的值设置在规则之上的某处)。
2) 配方中的每个命令都在其自己的子程序中执行shell; shell 变量值不 从一行到下一行保留:
all:
LAST_EXIT=5; echo the value is $$LAST_EXIT
@echo now the value is $$LAST_EXIT
这应该有效:
all:
- jekyll build relative || LAST_EXIT=$$?; \
if [ $$LAST_EXIT == 0 ]; then echo success!; fi
我对这个问题的解决方案是在 Makefile 中调用以下脚本来检查 Jekyll:
(称为 ./{scriptName
}
#!/bin/bash
LINEBREAK="*****************************************************************"
VERSION=0
echo "Checking Jekyll..."
VERSION=$(jekyll --version)
if test "$?" == "0"
then
echo "$VERSION is installed on this system..."
else
echo "$LINEBREAK"
echo "Oops! It looks like you don't have Jekyll yet, lets install it!"
echo "Running command: \"sudo gem install jekyll\""
echo "$LINEBREAK"
sudo gem install jekyll
if test "$?" != "0"
then
echo "$LINEBREAK"
echo "Jekyll install failed... It needs to be installed as a super-user on your system, which"
echo "requires your password. You can run \"sudo gem install jekyll\" yourself to install Jekyll."
echo "You can also see their website at: \"https://jekyllrb.com/docs/installation/\" for more information"
echo "$LINEBREAK"
exit 113
else
echo "$LINEBREAK"
echo "Jekyll has been installed on this system..."
echo "Proceeding to build..."
echo "$LINEBREAK"
fi
fi
exit 0