I want to use a ksh script to set up some environment variables that my makefile will later use. I tried to do:
setup:
. myscript
But it gives me errors like [[: not found
.
Is there a way to use an external script to load environment variables for make?
You could change the shell used in the makefile:
SHELL = /usr/bin/ksh # Or whatever path it's at
But it's probably a good idea to convert the script to something compatible with /bin/sh
(ideally completely POSIX-compatible) if you want it to work smoothly on other platforms.
Beware, this will probably not work as intended: as each Makefile command is executed in its own subshell, sourcing myscript
will modify only the local environment, not the whole Makefile's one.
example:
debug: setup
@echo "*** debug"
export | grep ENVVAR || echo "ENVVAR not found" #(a)
setup:
@echo "*** setup"
export ENVVAR=OK; export | grep ENVVAR || echo "ENVVAR not found" #(b)
export | grep ENVVAR || echo "ENVVAR not found" #(c)
output:
$ make debug
*** setup
export ENVVAR=OK; export | grep ENVVAR || echo "ENVVAR not found" #(b)
export ENVVAR='OK'
export | grep ENVVAR || echo "ENVVAR not found" #(c)
ENVVAR not found
*** debug
export | grep ENVVAR || echo "ENVVAR not found" #(a)
ENVVAR not found
As you can see, ENVVAR is found only in command (b), but commands (a) and (b) are executed in a new, clean environment.
来源:https://stackoverflow.com/questions/9502519/shell-script-to-export-environment-variables-in-make