2017-07-17 15 views
0

我需要验证与模式的ID创建文件(Abbbbb-YYY)之前正确验证身份的是如何使用的makefile

例子:

ID := A12345-789 B98765-123 C58730-417 
VARIANT := test1 test2 test3 

构建和后处理将生成的文件取决于起来的变体:

`sw_main_test1.hex ,sw_main_test1.hex and sw_main_test1.hex ` 

.PHONY : SW_TEST 
SW_TEST : 
    if <ID is correct> 
    cp sw_main_test1.hex --> A12345-789.hex 
    cp sw_main_test2.hex --> B98765-123.hex 
    cp sw_main_test3.hex --> C58730-417.hex 

我现在面临的问题与模式验证ID

`Abbbbb-yyy.txt` 

其中:A=[A-Z]; b=[0-9]; y=[0-9]

请让我知道如何验证ID为正确使用Makefile里的正则表达式使用任何工具或实用

+0

一个会怎么做,取决于一些其他因素。这些文件是针对自己的,还是仅仅是生成的,而另一个目标是作为副产品构建的?如果这些文件是自己的目标,我认为你的makefile是自己生成的,你最近怎么做?请阅读[mcve](https://stackoverflow.com/help/mcve)。 – Krom

+0

我建议将名称测试委托给脚本。你对哪些脚本语言感到满意? – Beta

+0

@Beta:我很喜欢python – Vicky

回答

0

在这个剧本,我认为,你从一个文件中获取您的ID (我在这里叫它someidcontent.txt)。然后你可以写这样的脚本(假设你只在Linux上工作)。

getID = $(shell cat someidcontent.txt) 

all: 
    if [ "$(getID)" == "1234567890" ]; then \ 
     cp -v output.txt ./delivery/$(getID).txt; \ 
    fi 

.PHONY: all 

编辑

我在前面的脚本犯了一个错误。我没有检查,如果ID是正确的。现在我的新脚本执行此操作:我从文件读取ID并检查其是否正确。如果ID是正确的,那么一些文件将被复制到具有ID号码的目标目录中。

# get ID from a file 
getID := $(shell cat someidcontent.txt) 
# need a hack for successful checking 
idToCheck := $(getID) 

# check procedure 
checkID := $(shell echo $(idToCheck) | grep "[A-Z][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]$$") 

all: 
ifeq "$(checkID)" "$(idToCheck)" 
    echo found 
    cp -v output.txt ./delivery/$(idToCheck).txt; 
endif 

.PHONY: all 

编辑2

好吧,这是一个有点挑战性,但我解决它在某种程度上。也许还有其他方法可以更好地解决这个问题。在我的解决方案,我认为有ID和源文件名文件看起来像这样(换句话说,这是我someidcontent.txt内容):

A2345-678:output1.txt 
B3456-123:output.txt 
C0987-987:thirdfile.txt 

,这是我的makefile有更多的解释意见。我希望,它们足以

# retrieve id and filename data from other file 
listContent := $(shell cat someidcontent.txt) 

# extract only IDs from other files 
checkIDs = $(shell echo $(listContent) | grep -o "[A-Z][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]") 

all: 
# iterate only over IDs 
# first, give me the ID 
# second retrieve the filename part for successful copy procedure 
# and copy the file to the target dir with ID as filename 
    @$(foreach x,$(checkIDs), \ 
     echo $(x); \ 
     cp -v $(shell echo $(listContent) | grep -o "$(x):[A-Z0-9a-z\.]*" | sed "s/[-A-Z0-9]*://g") ./delivery/$(x).t$ 
    ) 

.PHONY: all 
+0

Kristain你的答案是好的,但它会工作,如果我们有单身份证和单一输出文件,如果我们有IDS列表和输出文件列表将失败 – Vicky

+0

@Vicky:有没有办法如何确定哪个输出文件属于哪个ID? – kristian

+0

@kristain其序列第一个ID列表元素对应第一个VARIANT列表元素 – Vicky

0

您可以查看简单的字符串模式相当确定(不想说“很好”),从make内:

[A-F] := A B C D E F# 
[a-f] := a b c d e f# 
[A-Z] := $([A-F]) G H I J K L M N O P Q R S T U V W X Y Z# 
[a-z] := $([a-f]) g h i j k l m n o p q r s t u v w x y z# 
[0-9] := 0 1 2 3 4 5 6 7 8 9# 

###################################################################### 
##### $(call explode,_stringlist_,_string_) 
## Insert a blank after every occurrence of the strings from _stringlist_ in _string_. 
## This function serves mainly to convert a string into a list. 
## Example: `$(call explode,0 1 2 3 4 5 6 7 8 9,0xl337c0de)` --> `0 xl3 3 7 c0 de` 
explode = $(if $1,$(subst $(firstword $1),$(firstword $1) ,$(call explode,$(wordlist 2,255,$1),$2)),$2) 


ID := A12345-789 B98765-123 C58730-417 123456+328 

############################################################ 
# $(call check-id,_id-string_) 
# Return 'malformed' or the given id 
check-id = $(if $(call check-id-1,$(call explode,- $([A-Z]) $([0-9]),$1)),malformed,$1) 
check-id-1 = $(strip $(filter-out $([A-Z]),$(wordlist 1,1,$1)) $(filter-out $([0-9]),$(wordlist 2,6,$1)) $(filter-out -,$(word 7,$1)) $(filter-out $([0-9]),$(wordlist 8,10,$1))) 

$(info $(foreach w,$(ID),$(call check-id,$(w)))) 
相关问题