How to return the first character of a variable in gmake

江枫思渺然 提交于 2019-12-10 15:16:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!