简体   繁体   中英

Shell command as a variable in Makefile

I have the following (very simplified) Makefile:

# Makefile

MAKESH=shell
USER=$($MAKESH logname)

which aims to store my login name in the USER variable as if the $(shell logname) command was invoked. However this doesn't happen. I suspect that it's due to the way make evaluates variables.

Does anyone have some more insight to this or a possible workaround ie explicitly tell make that $MAKESH is actually the shell command?

Thanks.

Edit

Consider the following example:

#parent.mk

...
include child.mk
...

-------------------
#child.mk

export $(shell logname)/path1
export $(shell logname)/path2

-------------------
#bash_script.sh

...
source child.mk
...

(in this example, sourcing child.mk fails)

Essentially, what I wanted to do is to create a file that exports pathnames depending on the current user. The syntax should be valid both for makefiles and bash scripts, since it can be included/sourced from both places.

I also wanted to do subtle changes to these files, since they are part of a larger project and the interconnections are a bit confusing.

I have finally decided to go with this:

#parent.mk

...
export logname=$(shell logname)
include child.mk
...

-------------------
#child.mk

export $(logname)/path1
export $(logname)/path2

-------------------
#bash_script.sh

...
source child.mk
...

which is quite hacky, a tiny bit dangerous, but works. If you know a more elegant way/aprroach, let me know.

Finally, thank you for your solutions (and your warnings), which are valid for the pre-editted question.

The syntax should be

USER = $(shell logname)

I don't know why you want to put "shell" in a variable and screw up the function syntax, but if you really insist, try this:

$(eval USER=$$($MAKESH logname))

The $(eval ...) function will just turn it into the first form above, though, so it's wasted effort. (Unless your really trying to do something trickier than your example suggests.)

MAKESH=shell
$(eval USER=$$($(MAKESH) logname))
$(info USER is $(USER))

Avoid if possible.

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