How do I split a pathname-like string for use in a

2019-08-20 09:18发布

问题:

I have this Makefile code:

%.spf:
    @echo $@;
    runMe.sh $1 $2 $3 $4 $5;

An example value of the %.spf would be:

ros_apple/bananas_go_while_197815:123.0/monkey_110_worst_forever/thestar.spf

How would I go about extracting the arguments of the filename into variables?

$1 = ros_apple
$2 = bananas_go_while_197815:123.0
$3 = worst
$4 = 110
$5 = thestar

Is it possible to stay within the Makefile or do I have to create some Perl script that takes in the $@ and does the shell calling within?

回答1:

Do you really need the path components in a variable or do you just need to offer them to runMe.sh split? If the latter, then you can get it done with just sed and tr (or just sed). For example, saying make all with this:

X=ros_apple/bananas_go_while_197815:123.0/monkey_110_worst_forever/thestar.spf
all:
        @echo `echo $X | sed 's:\.spf$$::' | tr '/' ' '`

produces:

ros_apple bananas_go_while_197815:123.0 monkey_110_worst_forever thestar

So maybe try this:

%.spf:
        @echo $@
        runMe.sh `echo $X | sed 's:\.spf$$::' | tr '/' ' '`

If you're dead set on doing it in Perl then you can convert the sed and tr to Perl pretty easily.



回答2:

However ugly this is, you can probably do nested calls to dir for components 1 through 4, and notdir for the basename, and possibly use a pattern match to strip off the final slash.