Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert a Quoted String to a Normal One in Makefile?

Tags:

makefile

I am not sure whether I have described the question properly, but currently I am solving this problem in the following way

QUOTEDSTR := "hello world"
NORMALSTR := $(shell echo $(QUOTEDSTR))

Is there a more built-in way that 'make' can do this without calling shell? Thanks

like image 239
Danqi Wang Avatar asked May 03 '12 03:05

Danqi Wang


People also ask

How do you remove quotes from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you split a string with a quote?

Use method String. split() It returns an array of String, splitted by the character you specified.

How do you split a string with double quotes in Python?

You can escape quotes by preceding them with a \ like so : my_var = "Tabvxc \"avcx\"sdasaf\" sadasfdf. sdsadsaf '0000000000000000000000000000000'."


2 Answers

Another option:

NORMALSTR := $(patsubst "%",%,$(QUOTEDSTR))

Beta's answer will remove every quote in the string. The above solution will ONLY remove quotes that appear at the beginning and end. For example:

QUOTEDSTR := -DTITLE=\"Title\"

Beta's answer will result in a value of -DTITLE=\Title\ while using the patsubst solution this value will not be changed.

It depends on what you want.

EDIT

If you want to handle whitespace and still only match quotes at the beginning/end of the variable as per @stefanct's comment, you'll have to play some tricks. First you need to find a non-whitespace character which you know will never appear in your string. Let's choose ^ but you can choose something else if you want.

The algorithm is: convert all spaces to this character, then remove the quotes from the resulting single "word", then convert all instances of that character back to spaces, like this:

# Get a variable S that contains a single space
E :=
S := $E $E

NORMALSTR := $(subst ^,$S,$(patsubst "%",%,$(subst $S,^,$(QUOTEDSTR))))

Of course there are still complications; this handles only spaces for example, not other whitespace characters like TAB.

like image 134
MadScientist Avatar answered Oct 06 '22 01:10

MadScientist


All answers yet have issues. patsubst "[f]inds whitespace-separated words" so the simple solution by @MadScientist does not work for strings like "hello world". The one by @Beta on the other hand removes all quote characters no matter where they are.

The code below shows how to deal with strings with spaces in them as well. However, it will also remove other quote characters on the edge of words, for example "hello "world"3" will be transformed to hello world"3. If that's any better... I don't know, probably not.

Instead of the other solutions this one creates a user function named unquote instead of directly replacing the strings.

quoted="hello world"

unquote = $(patsubst "%,%,$(patsubst %",%,$(1)))
#unquote = $(subst $\",,$(1))
#unquote = $(patsubst "%",%,$(1))
#unquote = $(shell echo $(1))

unquoted = $(call unquote,$(quoted))

$(info Variable quoted is $(quoted))
$(info Variable unquoted is $(unquoted))

This simply looks for all quote characters at the start and end of every (white-space separated) word and removes it.

like image 36
stefanct Avatar answered Oct 05 '22 23:10

stefanct