makefile执行shell脚本

语法

$(shell pwd)

shell是函数名,后面的pwd是参数,返回的结果赋值给LDIR

CMD前面加@表示执行的时候不显示该命令,只显示命令的输出

当makefile的文件名不是makefile或者Makefile的时候,用make -f 文件名

注:只是定义变量的话,这个shell不会被执行,执行当这个变量被CMD执行的时候,这个shell才会被执行

[root@system CProgram]# cat test.mk
#test.mk
LDIR=$(shell pwd)
LSS=$(shell ls)
test:
	@echo $(LDIR)
	@echo $(LSS)
[root@system CProgram]# make -f test.mk
/home/gaole/CProgram
test.mk
[root@system CProgram]# 
[root@system CProgram]# cat test.mk
#test.mk
LDIR=$(shell pwd)
LSS=$(shell ls)
test:
	echo $(LDIR)
	echo $(LSS)
[root@system CProgram]# make -f test.mk
echo /home/gaole/CProgram
/home/gaole/CProgram
echo test.mk
test.mk

 @-中的-表示,即使执行错误,也要继续执行下面的CMD

root@system CProgram]# cat test.mk
#test.mk
LDIR=$(shell pwd)
LSS=$(shell ls)
TMP=$(shell echo "test">test)
test:
	@echo $(LDIR)
	@echo $(LSS)
    @-echo $(TMP)
[root@system CProgram]# make -f test.mk
make: `test' is up to date.
[root@system CProgram]# ls
test  test.mk
[root@system CProgram]# cat test
test
[root@system CProgram]# 

 添加一个复杂shell脚本

加上.PHONY:test,执行test是伪目标

[root@system CProgram]# make -f test.mk test
make: `test' is up to date.
[root@system CProgram]# vim test.mk
[root@system CProgram]# cat test.mk
#test.mk
LDIR=$(shell pwd)
LSS=$(shell ls)
TMP=$(shell echo "test">test)
OUT:=out
INIT=$(shell if [ ! -d $(OUT) ];then mkdir $(OUT);fi;)
test:
	@echo $(INIT)	
	@echo $(LDIR)
	@echo $(LSS)
	@-echo $(TMP)
.PHONY:test
[root@system CProgram]#
[root@system CProgram]# 
[root@system CProgram]# 
[root@system CProgram]# make -f test.mk
INIT
/home/gaole/CProgram
test test.mk

[root@system CProgram]# cat test.mk
#test.mk
LDIR=$(shell pwd)
LSS=$(shell ls)
TMP=$(shell echo "test">test)
OUT:=out
INIT=$(shell if [ ! -d $(OUT) ];then mkdir $(OUT);fi;)
test:
	@echo $(INIT)	
	@echo $(LDIR)
	@echo $(LSS)
	@-echo $(TMP)
.PHONY:test
[root@system CProgram]# 
[root@system CProgram]# 
[root@system CProgram]# ls
test  test.mk
[root@system CProgram]# make -f test.mk

/home/gaole/CProgram
out test test.mk

[root@system CProgram]# ls
out  test  test.mk

 根据目录名称直接生成TARGET,需要嵌套使用两个函数

TARGET=$(notdir $(shell pwd))

用途取出 /home/leg/CProgram中的CProgram

[root@system CProgram]# cat test.mk
#test.mk
LDIR=$(shell pwd)
TARGET=$(notdir $(shell pwd))
LSS=$(shell ls)
TMP=$(shell echo "test">test)
OUT:=out
INIT=$(shell if [ ! -d $(OUT) ];then mkdir $(OUT);fi;)
test:
	@echo $(INIT)	
	@echo $(LDIR)
	@echo $(LSS)
	@-echo $(TMP)
	@echo $(TARGET)
.PHONY:test
[root@system CProgram]# make -f test.mk

/home/leg/CProgram
out test test.mk

CProgram
[root@system CProgram]# pwd
/home/leg/CProgram
[root@system CProgram]# 

    原文作者:johnmand
    原文地址: https://blog.csdn.net/johnmand/article/details/123183376
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞