Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 从make file中的命令获取值_Shell_Makefile_Sh_Gnu Make_Gnu - Fatal编程技术网

Shell 从make file中的命令获取值

Shell 从make file中的命令获取值,shell,makefile,sh,gnu-make,gnu,Shell,Makefile,Sh,Gnu Make,Gnu,我有一个脚本,可以正常工作,我使用它来获得一些价值: tmpDir=$(xtr execute run) 在命令代码本身中,我打印值如下 fmt.Print("the dir is: ",dir) 然后,当我在脚本中放入echo时,我得到tmpDir 现在我需要切换到使用生成文件,我就是这样做的 我在tmpDir中没有得到任何值。我错过了什么 这是生成文件 all: app app_2 PROJ_DIR=$(PWD) # Create folder for build artifac

我有一个脚本,可以正常工作,我使用它来获得一些价值:

tmpDir=$(xtr execute run)
在命令代码本身中,我打印值如下

fmt.Print("the dir is: ",dir)
然后,当我在脚本中放入echo时,我得到
tmpDir

现在我需要切换到使用生成文件,我就是这样做的 我在
tmpDir
中没有得到任何值。我错过了什么

这是生成文件

all: app app_2
    PROJ_DIR=$(PWD)
# Create folder for build artifacts
    DIR=$(xtr execute run)
.PHONY: app
app:
    @echo $(DIR)

       ....

Make配方首先通过Make展开,然后传递到shell。因此,您的
DIR=$(xtr execute run)
首先由make展开,但由于没有名为
xtr execute run的make变量,因此它展开为
DIR=

但这不是您唯一的问题:配方中名为
DIR
的变量是shell变量,而不是make变量。它仅存在于make将生成以执行配方的这一部分(且仅此部分)的shell上下文中:

DIR=$(xtr execute run)
由于此shell调用中没有其他内容,因此它是无用的。这就像是在生成一个新的shell,在其中执行
DIR=
,然后退出

app
配方中的
DIR
变量是另一个同名变量。同样,make会在将配方部分传递给shell之前展开它,但由于没有这样的make变量,它会展开为:

echo
$(PWD)
也有类似的问题:它是通过make展开的,由于没有这样的make变量,它会展开为空字符串。您可以使用
CURDIR
,这是一个内置的make变量。但是请记住,
PROJ_DIR
是一个shell变量,它只存在于配方的这一行中

您可以尝试以下方法,而不是所有这些方法:

DIR := $(shell xtr execute run)
PROJ_DIR := $(CURDIR)

all: app app_2

.PHONY: app
app:
    @echo $(DIR)

这里,
DIR
PROJ_DIR
是make解析make文件时分配一次的make变量(注意
:=
,这很重要)。从现在起,它们可以在配方中使用,在配方的一部分实际传递给shell执行之前,它们将通过make进行扩展。

您尝试过什么?你发现了什么具体的问题?@Flimzy-这是我尝试的,但我在
makefile
@Flimzy-在脚本中得到了空值,但在makefile中没有找到完全相同的代码…@Flimzy-我使用制表符并能够执行它,这不是问题:)什么是
xtr
?这就是你要运行的命令吗?如果是这样,你应该使用。