Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make: how to replace character within a make variable?

Tags:

makefile

I have a variable such :

export ITEM={countryname}

this can be :

   "Albania", 
   "United States"   // with space
   "Fs. Artic Land"  // dot
   "Korea (Rep. Of)" // braket
   "Cote d'Ivoir"    // '

This variable $(ITEM) is passed to other commands, some needing is as it (fine, I will use $(ITEM)), some MUST HAVE characters replacements, by example, to go with mkdir -p ../folder/{countryname} :

   "Albania"          // => Albania
   "United States"    // => United_States
   "Fs. Artic Land"   // => Fs\._Artic_Land
   "Korea (Rep. Of)"  // => Korea_\(Rep\._Of\)
   "Cote d'Ivoire"    // => Cote_d\'Ivoire 

So I need a new make variable such

export ITEM={countryname}
export escaped_ITEM=$(ITEM).processed_to_be_fine

How should I do this characters replacements within my makefile ? (to keep things simple and not have to do an external script). I was thinking to use some transclude tr or something.

Note: working on Ubuntu.

like image 745
Hugolpz Avatar asked May 16 '26 04:05

Hugolpz


1 Answers

You can use the subst function in GNU Make to perform substitutions.

escaped_ITEM := $(subst $e ,_,$(ITEM))

(where $e is an undefined or empty variable; thanks to @EtanReisner for pointing it out).

You will need one subst for each separate substitution, though.

If at all possible, I would advise against this, however -- use single, machine-readable tokens for file names, and map them to human readable only as the very last step. That's also much easier in your makefile:

human_readable_us=United States
human_readable_kr=Korea (Rep. of)
human_readable_ci=Côte d'Ivoire
human_readable_tf=FS. Antarctic Lands

stuff:
        echo "$(human_readable_$(ITEM))"
like image 138
tripleee Avatar answered May 18 '26 17:05

tripleee