创建以下makefile以生成两个文件的差异, fiel1 和 file2 :
.PHONY: patch
patch:
diff file1 file2 > file12.patch
file1 的内容:
xxx
和 file2 :
xxx
yyy
但是当我发出make patch
时,错误来了:
diff file1 file2 > file12.patch
Makefile:3: recipe for target 'patch' failed
make: *** [patch] Error 1
但是,可以通过以下方式在终端中生成补丁:
diff file1 file2 > file12.patch
。
file12.patch 的内容:
1a2
> yyy
令人惊讶的是,我回去检查文件夹,make patch
确实产生了正确的补丁文件。
我无法理解的是make错误?例如这个错误肯定会停止make过程,之后会跳过所有命令。
有人可以解释这种行为吗?谢谢!
答案 0 :(得分:2)
上面引用的makefile无法创建:def AI():
# you can use r'' to specify a raw string and avoid using '\\' to escape '\'
fpath = r'\\ph-fss1\Students\S39055\Desktop\names.txt'
# this is called a 'context manager'
# when you are done with your operations, the file will close automatically
# the 'r+' mode opens for reading and writing
with open(fpath, 'r+') as f:
# this is list comprehension and I assume each name is on a new line
# a set is a container for unique values
# assuming that you will not have multiple of the same names
# if so, how do you plan to account for them?
names = set([line for line in f])
print "Hello and welcome to the Creative, Orginal, Reactive, A.I, Cora."
name = raw_input("What is your name? ")
if name in names:
print "Welcome back " + name
else:
print "You are a new user, would you like to me to remember your name?"
# this is fine, but what happens I just put in 'y' or 'n'?
# look for methods to handle invalid input
choice = raw_input("Yes/No: ").lower()
if choice == "yes":
# because we opened the file in 'r+', we seek(0) which puts us at the top
# then when we writelines(list(names)) we overwrite the entire file
# and store the original data with the new name as well
file.seek(0)
names.add(name)
# writelines() just writes an iterable versus a string
file.writelines(list(names))
目标的配方会被忽略。它必须是这样的:
.PHONY
通过查看退出代码,确定命令成功或失败。如果退出代码为0,则make假定命令成功。如果它是非0,则假定它失败。
您看到的错误意味着.PHONY: patch
patch:
diff file1 file2 > file12.patch
命令以非0退出代码退出。
diff手册说:
退出状态为0表示未发现差异,1表示一些差异 发现了差异,2意味着麻烦。
所以,既然你的diff确实发现了一些差异,那么它会以1的代码退出,这使得解释失败了。您可能希望将配方更改为:
diff
所以如果.PHONY: patch
patch:
diff file1 file2 > file12.patch || [ $? -eq 1 ]
失败,你会进一步检查退出代码是否为2。