我需要从长路径中分离文件夹名称,例如:../../folder1/folder2 -> folder2
。如果是文件名 - 可以使用nodir函数。但是nodir不适用于目录。
我尝试使用以下方法:
LIBNAMES := echo $(LIBS) | sed -e 's/\\S*\///g'
其中LIBS是长路径文件夹的列表。实施后,我的第一印象是它的工作原理(印刷了正确的LIBNAMES结果)。但是我的makefile中的以下LIBNAMES用法以奇怪的错误结束:似乎sed命令本身以某种方式被添加到结果中。那么,我的错误在哪里?
答案 0 :(得分:2)
为什么你说notdir
不能用于目录?它适用于任何路径。
all: ; echo $(notdir $(CURDIR))
答案 1 :(得分:0)
$ CURDIR="../../folder1/folder2"
$ echo ${CURDIR##*/}
folder2
##
的说明(来自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.