Pergunta

I want to print out a string variable in upper case. I'm trying to use the m4_toupper macro, but my variable seems to be ignored.

For example, given the following code:

foobar="linux-gnu"
echo "${foobar}"
echo m4_toupper("x${foobar}")
echo "${foobar}"

Results in the following output:

linux-gnu
X
linux-gnu

Since the x is capitalized, I suspect that the m4 macro is working correctly, but perhaps not receiving my variable string -- yet, the echo statements seem to work fine. Why is an empty string being returned?

Foi útil?

Solução 2

Your macro isn't ignored, it just gets evaluated at a different time than you're expecting. The M4sugar macros are evaluated when configure is created. You seem to want to have the toupper function applied when configure is run. You can do it at creation time by something like:

m4_define([thestring], [linux-gnu])dnl
m4_define([thexstring], [x])dnl
m4_append([thexstring], m4_toupper(thestring))dnl
foobar="thestring"
echo "${foobar}"
echo "thexstring"
echo "${foobar}"

but this won't help if foobar is set up at runtime. Then you'll have to resort to one of the runtime techniques suggested by Fredrik Pihl (or something similar).

In any case, m4_toupper("x${foobar}") is changed to "X${FOOBAR}" so that's why it's not appearing, since ${FOOBAR} is not defined in the environment.

Outras dicas

Don't know about m4 macro, but here are some ways to convert a variable to uppercase:

$ echo $foobar | awk '{print toupper($0)}'
LINUX-GNU

$ echo $foobar | tr '[a-z]' '[A-Z]'
LINUX-GNU

$ echo ${foobar^^}
LINUX-GNU
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top