如何在Makefile中计算范围

时间:2017-09-18 21:12:50

标签: linux makefile gnu

这是我的Makefile和输出,如何打印像[1-5, 7, 9-10]

这样的范围
$ ls /tmp/foo
foo10.txt  foo1.txt  foo2.txt  foo3.txt  foo4.txt  foo5.txt  foo7.txt  foo9.txt

$ cat Makefile 
DIR1 := /tmp/foo/
COMMA :=,
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
VERSIONS := $(subst $(SPACE),$(COMMA),$(patsubst foo%,%,$(basename $(notdir $(wildcard $(DIR1)/foo*.txt)))))
all:
        $(info versions is [${VERSIONS}])

$ make
versions is [10,1,2,3,4,5,7,9]

2 个答案:

答案 0 :(得分:2)

有几种方法:

  1. 如果您知道最大可能值:首先,添加using Microsoft.AspNetCore.Http.Connections; .... var httpContext = Context.GetHttpContext(); 以便支撑扩展有效。然后使用SHELL=bash之类的内容,并通过$(shell {1..10})传递结果,以排除不存在的文件。
  2. 再次在$(wildcard)
  3. 中排行sort -n
  4. 使用$(shell) s在左侧填充数字,然后正常排序。

答案 1 :(得分:0)

以下是完成工作的shell命令管道:

printf '%s\n' foo*[0-9].txt |
sed 's/[^0-9]*//g' |
sort -n |
awk 'function prnt(sep){printf "%s%s", s, (p > s ? "-" p : "") sep}
NR==1{s = $1} p < $1-1{prnt(","); s = $1} {p = $1} END{prnt(ORS)}'

1-5,7,9-10

命令是:

  1. printf在不同的行上打印每个文件名
  2. sed删除除数字之外的所有内容
  3. sort以数字方式排序以获得正确顺序的范围
  4. awk设置范围并格式化结果