[英]How Validate Id is correct before creating file using makefile
我需要使用模式(Abbbbb-yyy)驗證ID
范例:
ID := A12345-789 B98765-123 C58730-417
VARIANT := test1 test2 test3
構建和后期處理將生成文件,具體取決於VARIANTS:
`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=[AZ]; b=[0-9]; y=[0-9]
A=[AZ]; b=[0-9]; y=[0-9]
請讓我知道如何使用任何工具或實用工具在Makefile中使用正則表達式來驗證ID是否正確
我假設在此腳本中,您是從文件獲取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
您可以從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))))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.