简体   繁体   English

用于在make中导出环境变量的Shell脚本

[英]Shell script to export environment variables in make

I want to use a ksh script to set up some environment variables that my makefile will later use. 我想使用ksh脚本来设置一些我的makefile以后将使用的环境变量。 I tried to do: 我试着这样做:

setup:
   . myscript

But it gives me errors like [[: not found . 但它给了我一些错误,如[[: not found

Is there a way to use an external script to load environment variables for make? 有没有一种方法可以使用外部脚本来加载make的环境变量?

You could change the shell used in the makefile: 您可以更改 makefile中使用的shell

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. 但是 ,如果您希望它在其他平台上顺利运行,那么将脚本转换为与/bin/sh兼容的东西(理想情况下完全与POSIX兼容)可能是个好主意。

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. 请注意,这可能无法按预期工作:由于每个Makefile命令都在其自己的子shell中执行,因此采购myscript仅会修改本地环境,而不会修改整个Makefile的环境。

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. 如您所见,ENVVAR仅在命令(b)中找到,但命令(a)和(b)在新的干净环境中执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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