在GNU中,如何将一个变量转换为小写?

时间:2021-02-14 20:22:29

This is a silly question, but.... with GNU Make:

这是一个愚蠢的问题,但....GNU使:

VAR = MixedCaseText
LOWER_VAR = $(VAR,lc)

default:
        @echo $(VAR)
        @echo $(LOWER_VAR)

In the above example, what's the correct syntax for converting VAR's contents to lower case? The syntax shown (and everything else I've run across) result in LOWER_VAR being an empty string.

在上面的例子中,将VAR的内容转换为小写的正确语法是什么?所显示的语法(以及我遇到的所有其他语法)导致LOWER_VAR为空字符串。

4 个解决方案

#1


43  

you can always spawn off tr

你总是可以衍生tr

LOWER_VAR = `echo $(VAR) | tr A-Z a-z`

or

LOWER_VAR  = $(shell echo $(VAR) | tr A-Z a-z)

The 'lc' functions you trying to call is from GNU Make Standard Library

您试图调用的“lc”函数来自GNU Make标准库

Assuming that is installed , the proper syntax would be

假设已安装,正确的语法将是。

LOWER_VAR  = $(call lc,$(VAR))

#2


40  

You can do this directly in gmake, without using the GNU Make Standard Library:

您可以在gmake中直接这样做,无需使用GNU Make标准库:

lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))

VAR = MixedCaseText
LOWER_VAR = $(call lc,$(VAR))

all:
        @echo $(VAR)
        @echo $(LOWER_VAR)

It looks a little clunky, but it gets the job done.

它看起来有点笨拙,但它完成了任务。

If you do go with the $(shell) variety, please do use := instead of just =, as in LOWER_VAR := $(shell echo $VAR | tr A-Z a-z). That way, you only invoke the shell one time, when the variable is declared, instead of every time the variable is referenced!

如果使用$(shell)类型,请使用:=而不是=,如小写_var:= $(shell echo $VAR | tr A-Z A-Z)。这样,您只在声明变量时调用shell,而不是每次引用变量时调用它!

Hope that helps.

希望有帮助。

#3


15  

To handle capital letters with accents:

处理带有口音的大写字母:

LOWER_VAR  = $(shell echo $VAR | tr '[:upper:]' '[:lower:]')

Results:

结果:

$ VAR="Éclipse"
$ echo $VAR | tr A-Z a-z
Éclipse
$ echo $VAR | tr '[:upper:]' '[:lower:]'
éclipse

#4


3  

I find this slightly cleaner...

我觉得这个有点干净……

$(shell tr '[:upper:]' '[:lower:]' <<< $(VAR))

#1


43  

you can always spawn off tr

你总是可以衍生tr

LOWER_VAR = `echo $(VAR) | tr A-Z a-z`

or

LOWER_VAR  = $(shell echo $(VAR) | tr A-Z a-z)

The 'lc' functions you trying to call is from GNU Make Standard Library

您试图调用的“lc”函数来自GNU Make标准库

Assuming that is installed , the proper syntax would be

假设已安装,正确的语法将是。

LOWER_VAR  = $(call lc,$(VAR))

#2


40  

You can do this directly in gmake, without using the GNU Make Standard Library:

您可以在gmake中直接这样做,无需使用GNU Make标准库:

lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))

VAR = MixedCaseText
LOWER_VAR = $(call lc,$(VAR))

all:
        @echo $(VAR)
        @echo $(LOWER_VAR)

It looks a little clunky, but it gets the job done.

它看起来有点笨拙,但它完成了任务。

If you do go with the $(shell) variety, please do use := instead of just =, as in LOWER_VAR := $(shell echo $VAR | tr A-Z a-z). That way, you only invoke the shell one time, when the variable is declared, instead of every time the variable is referenced!

如果使用$(shell)类型,请使用:=而不是=,如小写_var:= $(shell echo $VAR | tr A-Z A-Z)。这样,您只在声明变量时调用shell,而不是每次引用变量时调用它!

Hope that helps.

希望有帮助。

#3


15  

To handle capital letters with accents:

处理带有口音的大写字母:

LOWER_VAR  = $(shell echo $VAR | tr '[:upper:]' '[:lower:]')

Results:

结果:

$ VAR="Éclipse"
$ echo $VAR | tr A-Z a-z
Éclipse
$ echo $VAR | tr '[:upper:]' '[:lower:]'
éclipse

#4


3  

I find this slightly cleaner...

我觉得这个有点干净……

$(shell tr '[:upper:]' '[:lower:]' <<< $(VAR))