问题描述
来自《 Bash初学者指南》表7-1 ,我们可以说,如果 FILE1
早于 FILE2
,则 [FILE1 -ot FILE2]
将产生 True ,或者如果 FILE2
存在而 FILE1
不存在.如果缺少 FILE1
并且存在 FILE2
,则在shell上执行此命令将返回 True .但是,当我在make脚本中使用它时,只要不存在 FILE1
且存在 FILE2
,它就会产生 False .确切地说,我在make和shell脚本中使用了以下命令:
From Table 7-1 of Bash Guide for Beginners, we can say that [ FILE1 -ot FILE2 ]
will yield True if FILE1
is older than FILE2
, or if FILE2
exists and FILE1
does not. Executing this command on shell returns True if FILE1
is absent and FILE2
is present. However, when I made use of this in my make script, it yielded False whenever FILE1
was absent and FILE2
was present. To be precise, I made use of the following command in my make and shell scripts:
[FILE1 -ot FILE2]&&回声1 ||回声0
在shell上运行此命令时,只要期望它就会返回1.但是,仅当 FILE1
存在并且被发现早于 FILE2
时,通过make运行此命令的结果为1.在其他情况下,例如,每当未发现 FILE1
早于 FILE2
时,或者每当 FILE1
不存在且 FILE2
存在,上面的命令在我的make脚本中产生0.请注意,在我的make脚本中,我将上述命令的结果存储在全局变量中.稍后,该全局变量将在条件之一中用于条件.
Running this command on shell returned 1 whenever it was expected to. However, running this command via make resulted in 1 only when FILE1
existed and was found to be older than FILE2
. In other scenarios, i.e. whenever FILE1
was not found to be older than FILE2
OR whenever FILE1
was absent and FILE2
existed, the above command yielded 0 in my make script. Note that in my make script, I'm storing the results of the above command in a global variable. This global variable is later used in one of the recipes by a conditional.
所以,我想知道这是make中的错误/功能还是我的最终可能出现的问题(由于某些拼写错误或其他可能的错误来源)?
So, I wish to know whether this is a bug/feature in make OR a possible issue from my end(due to some typo or other possible sources of error)?
注意:我正在Ubuntu 18.04.3上使用GNU Make 4.1
Note : I'm using GNU Make 4.1 on Ubuntu 18.04.3
推荐答案
尝试此实验(在脚本中):
Try this experiment (in a script):
在我测试的情况下:当file1不存在而当file2存在时:
In the case I tested: when file1 does not exist and when file2 does exist:
#!/bin/sh
[ file1 -ot file2 ] && echo "file1 older" || echo "file2 older"
使文件2变老
#!/bin/bash
[ file1 -ot file2 ] && echo "file1 older" || echo "file2 older"
使文件1变老
默认情况下,make将使用#!/bin/sh作为外壳,您可以通过以下方法对此进行更改: SHELL =/bin/bash
在您的makefile中
By default make will use #!/bin/sh as the shell, you can change this by: SHELL = /bin/bash
in your makefile
我不确定为什么会有所不同-它只是sh和bash之间的不同实现方式...
I am not sure why this is different - its just a different implementation between sh and bash...
这篇关于执行[FILE1 -ot FILE2]在make和shell中产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!