问题
Using GNU's make, I'd like to extract the first character of a variable. Currently I'm using the shell function to have bash perform the substring. I'm wanting to know if there is a way using gmake's built-ins to do the same.
DIR=/user/$(shell echo "$${USER:0:1}")/$(USER)/
回答1:
It's not very satisfying, and you'd have to add to $(INITIALS) until you were happy, but:
INITIALS := a b c d e f g h i j k l m n o p q r s t u v w x y z
U := $(strip $(foreach a,$(INITIALS),$(if $(USER:$a%=),,$a)))
DIR = /user/$(U)/$(USER)/
Perhaps the sensible approach would be to take note of the := usages in the above, and amend your simple version to DIR := ...$(shell ...)... so that the shell command is only invoked once.
回答2:
http://www.gnu.org/software/make/manual/make.html#Functions is a comprehensive list of everything you can do with gmake builtins.
It does not appear to be possible to extract the first character without $(shell), unfortunately.
来源:https://stackoverflow.com/questions/3703881/how-to-return-the-first-character-of-a-variable-in-gmake