In GNU Make, how do I convert a variable to lower case?

前端 未结 5 1031
误落风尘
误落风尘 2020-12-08 12:45

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

VAR = MixedCaseText
LOWER_VAR = $(VAR,lc)

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


        
相关标签:
5条回答
  • 2020-12-08 13:24

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

    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!

    Hope that helps.

    0 讨论(0)
  • 2020-12-08 13:26

    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
    
    0 讨论(0)
  • 2020-12-08 13:34

    If Python is installed this runs even on Windows:

    $(shell python -c "print('$(VAR)'.lower())")
    
    0 讨论(0)
  • 2020-12-08 13:39

    you can always spawn off 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

    Assuming that is installed , the proper syntax would be

    LOWER_VAR  = $(call lc,$(VAR))
    
    0 讨论(0)
  • 2020-12-08 13:41

    I find this slightly cleaner...

    $(shell tr '[:upper:]' '[:lower:]' <<< $(VAR))
    
    0 讨论(0)
提交回复
热议问题