Makefile:如何从长路径中分离文件夹名称

时间:2014-02-19 17:12:38

标签: sed makefile

我需要从长路径中分离文件夹名称,例如:../../folder1/folder2 -> folder2。如果是文件名 - 可以使用nodir函数。但是nodir不适用于目录。

我尝试使用以下方法:

LIBNAMES := echo $(LIBS) | sed  -e 's/\\S*\///g'

其中LIBS是长路径文件夹的列表。实施后,我的第一印象是它的工作原理(印刷了正确的LIBNAMES结果)。但是我的makefile中的以下LIBNAMES用法以奇怪的错误结束:似乎sed命令本身以某种方式被添加到结果中。那么,我的错误在哪里?

2 个答案:

答案 0 :(得分:2)

为什么你说notdir不能用于目录?它适用于任何路径。

all: ; echo $(notdir $(CURDIR))

答案 1 :(得分:0)

$ CURDIR="../../folder1/folder2"

$ echo ${CURDIR##*/}
folder2

以下是bash中使用##的说明(来自bash手册的副本)

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.