简体   繁体   中英

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

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到4进行dir嵌套调用,对基名进行notdir嵌套调用,并可能使用模式匹配来去除最后的斜杠。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM