折腾:
【已解决】Makefile中目标中嵌套使用ifeq条件判断
期间,此处需要在makefile中的target目标中的命令行中实现条件判断。
去找找看
shell if condition in makefile
1 2 3 | @ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH);\ fi ;\ |
结果:
1 2 3 | # cd /Users/xxx/dev/crifan/gitbook/gitbook_template/books/mobile_network_evolution_history && \ # pwd /bin/sh : -c: line 0: syntax error near unexpected token ` then ' |
试试:
1 2 3 4 5 | git pull && \ @ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); fi ; # git status && \ |
结果:
1 2 3 4 | @ if [ true == true ]; then /bin/sh: -c: line 0: syntax error near unexpected token `then ' /bin/sh: -c: line 0: `cd /Users/xxx/dev/crifan/crifan.github.io && pwd && ls -la && git pull && @if [ true == true ]; then ' make: *** [commit] Error 2 |
试试:
1 2 3 | @ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \ fi ; \ |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 | git pull && \ @ if [ true == true ]; then \ python /Users/xxx/dev/crifan/gitbook/gitbook_template/common/tools/update_crifan_github_io_readme.py --localGithubIoPath /Users/xxx/dev/crifan/crifan.github.io; \ fi; \ # git status && \ # git add mobile_network_evolution_history/* && \ # git status && \ # git commit -m "1. update book mobile_network_evolution_history" && \ # git status && \ # git push && \ # cd /Users/xxx/dev/crifan/gitbook/gitbook_template/books/mobile_network_evolution_history && \ # pwd /bin/sh: -c: line 0: syntax error near unexpected token `then' |
还是会报,无法识别then
去掉空格:
1 2 3 | @ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH);\ fi ;\ |
结果:错误依旧。
试试单行
1 | @ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); fi ; |
结果:
错误依旧
试试:
1 | @ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ] then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH) fi |
结果:
可以运行,但结果报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 | ls -la && \ git pull && \ @ if [ true == true ] then python /Users/xxx/dev/crifan/gitbook/gitbook_template/common/tools/update_crifan_github_io_readme.py --localGithubIoPath /Users/xxx/dev/crifan/crifan.github.io fi /Users/xxx/dev/crifan/crifan.github.io total 16 drwxr-xr-x 59 xxx CORP\Domain Users 1888 9 16 17:42 . drwxr-xr-x 13 xxx CORP\Domain Users 416 8 20 14:03 .. drwxr-xr-x 15 xxx CORP\Domain Users 480 9 18 14:29 .git drwxr-xr-x 6 xxx CORP\Domain Users 192 9 16 17:59 5g_message_rcs_tech_summary 。。。 Already up to date. /bin/sh: @ if : command not found make: *** [commit] Error 127 |
很明显,不知此@if
改为:
1 | if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); fi ; |
结果:
可以运行了:
1 | localGithubIoPath= /Users/xxx/dev/crifan/crifan .github.io |
代码:
1 2 3 4 | args = parser.parse_args() print ( "args=%s" % args) localGithubIoPath = args.localGithubIoPath print ( "localGithubIoPath=%s" % localGithubIoPath) |
输出:
1 2 | args=Namespace(localGithubIoPath= '/Users/xxx/dev/crifan/crifan.github.io' ) localGithubIoPath= /Users/xxx/dev/crifan/crifan .github.io |
再去优化缩进:
1 2 3 4 | git pull && \ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \ fi ; |
结果:
也是可以的。
且改为false:
1 2 3 4 5 6 7 8 | ifeq ($(ENABLE_COMMIT_GITHUB_IO), true) # GITHUB_IO_PATH=/Users/crifan/dev/dev_root/github/github.io/crifan.github.io GITHUB_IO_PATH = / Users / xxx / dev / crifan / crifan.github.io ENABLE_UPDATE_GITHUB_IO_README = false # ENABLE_UPDATE_GITHUB_IO_README = true endif |
以及加上else
1 2 3 4 5 | if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \ else \ echo Ignored update README.md before commit $(BOOK_NAME) to github.io fi ; |
结果:
1 2 3 | else \ echo Ignored update README.md before commit mobile_network_evolution_history to github.io /bin/sh : -c: line 1: syntax error: unexpected end of file |
改为:
1 2 3 4 5 | if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \ else \ echo "Ignored update README.md before commit $(BOOK_NAME) to github.io" ; fi ; |
结果:错误依旧。
加上:
1 2 3 4 5 | if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \ else \ echo "Ignored update README.md before commit $(BOOK_NAME) to github.io" ; \ fi ; |
结果:
正常输出了:
1 | Ignored update README.md before commit mobile_network_evolution_history to github.io |
【总结】
此处在makefile中的target目标中,有:
1 2 3 4 5 6 | commit: 。。。 cd $(GITHUB_IO_PATH) && \ pwd && \ ls -la && \ git pull && \ |
这种,其实是命令行的逻辑,其中想要实现 条件判断,且用到了makefile中的变量判断
最后是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | commit: @echo ================================================================================ ifeq ($(ENABLE_COMMIT_GITHUB_IO), true ) @echo Commit for $(BOOK_NAME) # rsync $(RSYNC_PARAMS) $(RELEASE_PATH) $(GITHUB_IO_PATH) cd $(GITHUB_IO_PATH) && \ pwd && \ ls -la && \ git pull && \ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --localGithubIoPath $(GITHUB_IO_PATH); \ else \ echo "Ignored update README.md before commit $(BOOK_NAME) to github.io" ; \ fi; else @echo Ignored commit $(BOOK_NAME) to github.io endif |
即:
在commit的target中,\反斜杠中,即其实是命令行环境,实现条件判断,则是:
1 2 3 4 5 | if xxx; then \ do something; \ else \ do something else ; \ fi ; |
即可。
其本身其实就是:普通命令行中的写法,和makefile没啥关系。
其中引用此处的makefile中的变量,则也是普通的:$(var_in_makefile),即可。
转载请注明:在路上 » 【已解决】makefile中目标中实现命令行中的条件判断