如何在gmake中返回变量的第一个字符 [英] How to return the first character of a variable in gmake

查看:79
本文介绍了如何在gmake中返回变量的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GNU的make,我想提取变量的第一个字符.目前,我正在使用shell函数让bash执行子字符串.我想知道是否有一种方法可以使用gmake的内置功能来做到这一点.

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)/

推荐答案

这不是很令人满意,您必须添加到$(INITIALS)直到满意为止,但是:

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)/

也许明智的方法是记下上面的:=用法,并将您的简单版本修改为DIR := ...$(shell ...)...,以便只调用一次shell命令.

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.

这篇关于如何在gmake中返回变量的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆