在make version 4.2 {2016}上添加了变量.SHELLSTATUS
。但是我大多数的Linux使用的是make版本4.1
。我如何获得较早的make版本的make $(shell ...)命令的退出状态和输出结果?
使用make 4.2,我可以这样做:
newmake:
printf '%s\n' "$(shell command && \
( printf 'Successfully created the zip file!\n'; exit 0 ) || \
( printf 'Error: Could not create the zip file!\n'; exit 1 ) )"
printf 'Exit status: '%s'\n' "${.SHELLSTATUS}"
可以正确运行:
printf '%s\n' "Successfully created the zip file!"
Successfully created the zip file!
printf 'Exit status: '%s'\n' "0"
Exit status: 0
但是,如果我使用的是make 4.1或更早的版本,则没有.SHELLSTATUS
变量。然后,我试图像这样定义自己的.SHELLSTATUS
变量:
oldmake:
printf '%s\n' "$(shell command && \
( printf 'Successfully created the zip file!\n'; $(eval exit_status := 0) echo ${exit_status} > /dev/null ) || \
( printf 'Error: Could not create the zip file!\n'; $(eval exit_status := 1) echo ${exit_status} > /dev/null ) )"
printf 'Exit status: '%s'\n' "${exit_status}"
但是,它无法正常运行:
printf '%s\n' "Successfully created the zip file!"
Successfully created the zip file!
printf 'Exit status: '%s'\n' "1"
Exit status: 1
如您所见,正确的消息已打印,但退出状态应为0
,而不是1
。
消息Successfully created the zip file!
和Error: Could not create the zip file!
纯粹是说明性的,不能在$(shell ...)
之外使用。我在外壳程序中拥有的是功能全面的Python
脚本,该脚本将其结果输出到屏幕上:
define NEWLINE
endef
define RELEASE_CODE
from __future__ import print_function
import os
import zipfile
version = "${version}"
if not version:
print( "Error: You need pass the release version. For example: make release version=1.1", end="@NEWNEWLINE@" )
exit(1)
CURRENT_DIRECTORY = os.path.dirname( os.path.realpath( __file__ ) )
print( "Packing files on %s" % CURRENT_DIRECTORY, end="@NEWNEWLINE@" )
file_names = []
initial_file_names = [
"Makefile",
"build.bat",
"fc-portuges.def",
os.path.join("setup", "makefile.mk"),
os.path.join("setup", "scripts", "timer_calculator.sh"),
]
for direcory_name, dirs, files in os.walk(CURRENT_DIRECTORY):
if ".git" in direcory_name:
continue
for filename in files:
filepath = os.path.join( direcory_name, filename )
if ".git" in filepath or not ( filepath.endswith( ".tex" )
or filepath.endswith( ".bib" )
or filepath.endswith( ".pdf" ) ):
continue
file_names.append( filepath )
for filename in initial_file_names:
filepath = os.path.join( CURRENT_DIRECTORY, filename )
file_names.append( filepath )
zipfilepath = os.path.join( CURRENT_DIRECTORY, version + ".zip" )
zipfileobject = zipfile.ZipFile(zipfilepath, mode="w")
zipfilepathreduced = os.path.dirname( os.path.dirname( zipfilepath ) )
try:
for filename in file_names:
relative_filename = filename.replace( zipfilepathreduced, "" )
print( relative_filename, end="@NEWNEWLINE@" )
zipfileobject.write( filename, relative_filename, compress_type=zipfile.ZIP_DEFLATED )
except Exception as error:
print( "", end="@NEWNEWLINE@" )
print( "An error occurred: %s" % error, end="@NEWNEWLINE@" )
exit(1)
finally:
zipfileobject.close()
print( "", end="@NEWNEWLINE@" )
print( "Successfully created the release version on:@NEWNEWLINE@ %s!" % zipfilepath , end="" )
endef
all:
printf '%s\n' "$(shell echo \
'$(subst ${NEWLINE},@NEWLINE@,${RELEASE_CODE})' | \
sed 's/@NEWLINE@/\n/g' | python - && \
printf 'Successfully created the zip file!\n' || \
printf 'Error: Could not create the zip file!\n' )" | sed 's/@NEWNEWLINE@/\n/g'
现在它可以正常运行,但是我缺少python退出代码,可以使用此.SHELLSTATUS
进行修复:
all:
printf '%s\n' "$(shell echo \
'$(subst ${NEWLINE},@NEWLINE@,${RELEASE_CODE})' | \
sed 's/@NEWLINE@/\n/g' | python - && \
( printf 'Successfully created the zip file!\n'; exit 0 ) || \
( printf 'Error: Could not create the zip file!\n'; exit 1 ) )" | sed 's/@NEWNEWLINE@/\n/g'
exit "${.SHELLSTATUS}"
但是,它仅适用于品牌4.2
或更高版本。
相关:
答案 0 :(得分:2)
我完全不明白为什么您要尝试使用$(shell ...)
。这太过复杂了,因此毫无意义。 $(shell ...)
的唯一作用是它会在运行任何配方之前扩展。但是您永远不要尝试利用它。相反,将脚本直接移到配方内部看起来很自然。
例如,考虑一下:
unused = """
ifeq (true,false)
"""
# some python stuff
print("Hello world")
exit(1)
unused = """
endif
# here comes make stuff
.ONESHELL:
.PHONY: all
all:
@/usr/bin/python $(lastword $(MAKEFILE_LIST))
ec=$$?
echo "script exitcode is $$ec"
exit $$ec
#"""
也许更好的解决方案是简单地将python
设为单个规则的自定义SHELL
。虽然,在这种情况下,将python和shell脚本按相同的规则混合可能很棘手(但我认为这不是很重要)。
答案 1 :(得分:0)
我设法通过创建文件来做到这一点:
fixedmake:
printf '%s\n' "$(shell command && \
printf 'Successfully created the zip file!\n' || \
( printf 'Error: Could not create the zip file!\n'; \
printf 'error\n' > /tmp/has_errors ) )"
if [ -e /tmp/has_errors ]; \
then printf 'Exit with errror\n'; \
rm /tmp/has_errors; \
else \
printf 'Exit with success\n'; \
fi;
->
printf '%s\n' "Successfully created the zip file!"
Successfully created the zip file!
if [ -e /tmp/has_errors ]; \
then printf 'Exit with errror\n'; \
rm /tmp/has_errors; \
else \
printf 'Exit with success\n'; \
fi;
Exit with success
还有更好的选择吗?