Executing [ FILE1 -ot FILE2 ] yields different results in make and shell

眉间皱痕 提交于 2021-02-17 05:17:05

问题


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 ] && echo 1 || echo 0

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.

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)?

Note : I'm using GNU Make 4.1 on Ubuntu 18.04.3


回答1:


Try this experiment (in a script):

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"

yields file2 older

#!/bin/bash

[ file1 -ot file2 ] && echo "file1 older" || echo "file2 older"

yields file1 older

By default make will use #!/bin/sh as the shell, you can change this by: SHELL = /bin/bash in your makefile

I am not sure why this is different - its just a different implementation between sh and bash...



来源:https://stackoverflow.com/questions/63643950/executing-file1-ot-file2-yields-different-results-in-make-and-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!