Reputation: 2422
Given a variable, how can I create another variable with the leading repetitions of ../
that the first variable starts with, albeit with one less repetition?
For instance, given ../../../foo/bar
, how do I end up with ../../
?
I've tried a perl regexp like perl -e '$a = "../../../foo/bar"; $a =~ /^\.\.\/((\.\.\/)+)/; print "$1\n"'
but can't figure out the magic incantation of quotes, backslashes, dollar signs, and so on needed to run it within $(shell)
.
Further I suspect there's a simpler way to do it.
Upvotes: 0
Views: 44
Reputation: 2898
This will remove one ../
at the front: $(patsubst ../%,%,$(ORIGINAL_PATH))
To isolate the first streak of ../
one could go as follows:
space := $(strip) $(strip)#
FIRST_STREAK := $(firstword $(subst $(space)../,../,$(subst ../,../$(space),$(ORIGINAL_PATH))))
Let's write this as a function:
space := $(strip) $(strip)#
UP_PATH = $(patsubst ../%,%,$(firstword $(subst $(space)../,../,$(subst ../,../$(space),$1))))
Test it:
ORIGINAL_PATH := ../../../foo/bar/../baz
$(info $(call UP_PATH,$(ORIGINAL_PATH)))
Output:
../../
Upvotes: 1