I’m using make in Windows and I need to manipulate some variables to create a filename. I have a base filename and an optional part to append. If the optional part is appended an underscore should be inserted between two parts. The optional part is being read in from a file and contains 5 characters (spaces or alphanumeric).
This is what I expected to work:
OPTIONAL_NAME=" "
BASE_NAME=myfile
STRIPPED_OPTIONAL_NAME=$(strip $(OPTIONAL_NAME))
ifeq ("$(STRIPPED_OPTIONAL_NAME)","")
FULL_NAME=$(BASE_NAME)
else
FULL_NAME=$(BASE_NAME)_$(STRIPPED_OPTIONAL_NAME)
endif
OPTIONAL_NAME could have the following values:
OPTIONAL_NAME=" "
OPTIONAL_NAME="ABCDE"
OPTIONAL_NAME="A "
OPTIONAL_NAME=" A "
OPTIONAL_NAME=" A"
The value of OPTIONAL_NAME has quotes around it due to the way it is being imported (see my previous question – Read a value from a file into a variable).
The problem with the result is that strip seems to reduce multiple spaces to single spaces, but not actually remove leading or trailing spaces.
I’ve tried using this to strip the spaces:
space:=
space+=
STRIPPED_OPTIONAL_NAME=$(subst $(space),,$(OPTIONAL_NAME))
This does remove the spaces, but I struggle to check for an empty string.
Thanks
Stephen
You should not put the variable in the comparison in quotation marks. Try this instead:
The above checks for empty strings.
Or
to check for single space.