0

我正在尝试在 Makefile 中生成文件的 MD5 校验和。在我的 Makefile 我有类似的东西;

校验和=md5sum $(myfile)

但变量 CHECKSUM 始终为空

谁能告诉我这里有什么问题?

4

5 回答 5

2

这是一个稍微不同的示例,我将预期的 md5 值设置为make变量,然后在我的配方中的 shell 命令中检查它。在这种情况下,我想下载一个特定版本的 Anaconda 并在安装之前检查它的 md5sum。

生成文件:

SHELL:=/bin/bash
ANACONDA_MD5:=c989ecc8b648ab8a64731aaee9ed2e7e

none:

Anaconda3-5.0.1-Linux-x86_64.sh: 
    wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh

download: Anaconda3-5.0.1-Linux-x86_64.sh

verify: download
    AnacondaMD5="$$(md5sum Anaconda3-5.0.1-Linux-x86_64.sh | cut -d ' ' -f1)" && \
    if [ "$$AnacondaMD5" == '$(ANACONDA_MD5)' ]; then echo "md5sum matches"; fi

输出:

$ make verify
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
--2018-01-16 18:11:50--  https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
Resolving repo.continuum.io... 104.16.18.10, 104.16.19.10, 2400:cb00:2048:1::6810:130a, ...
Connecting to repo.continuum.io|104.16.18.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 550796553 (525M) [application/x-sh]
Saving to: `Anaconda3-5.0.1-Linux-x86_64.sh'

100%[====================================================================================================================>] 550,796,553  103M/s   in 6.8s

2018-01-16 18:11:59 (77.0 MB/s) - `Anaconda3-5.0.1-Linux-x86_64.sh' saved [550796553/550796553]

AnacondaMD5="$(md5sum Anaconda3-5.0.1-Linux-x86_64.sh | cut -d ' ' -f1)" && \
    if [ "$AnacondaMD5" == 'c989ecc8b648ab8a64731aaee9ed2e7e' ]; then echo "md5sum matches"; fi
md5sum matches

注意$$AnacondaMD5填充内联bash变量的用法与$(ANACONDA_MD5)填充make变量的用法

版本:

$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-redhat-linux-gnu
于 2018-01-16T23:15:17.863 回答
1

正如克里斯所说,你需要这样的东西:

CHECKSUM=$(md5sum $(myfile))

如果您不知道,CHECKSUM 将仅在该行上可用。即以下将输出一个空白链接:

test:
    CHECKSUM=$(md5sum $(myfile))
    echo $$CHECKSUM

以下将满足您的需要:

test:
    CHECKSUM=$(md5sum $(myfile)); echo $$CHECKSUM

或者,如果您需要多行

test:
    CHECKSUM=$(md5sum $(myfile)); \
    echo $$CHECKSUM; \
    echo $$CHECKSUM;

如果您剪切 n 粘贴上面的内容,则需要插入标签。

于 2011-05-05T22:47:56.463 回答
1

您是否需要命令部分之外的命令结果makefile
然后,如果您的makeis GNU-make$(shell)功能可用。
例如:

CHECKSUM := $(shell md5sum $(myfile))
于 2011-05-06T03:56:45.037 回答
1

这对我有用:

NOW=$$(date)
print-now:
    @echo $(NOW)
md5sum:
    @SUM=$$(md5sum file.txt | cut -d' ' -f 1); \
    echo $$SUM; \
    cp file.txt file.$${SUM}.txt; \

现在运行它make md5sum

你应该得到文件file.<sum>.txt

如果您从上面复制代码,请记住使用tab缩进或从 repo https://github.com/rofrol/makefile-md5sum获取文件。

于 2019-05-15T17:30:01.890 回答
0

有关文件列表,您可以在 Makefile 中进行以下操作

FILES=foo/bar/image.svg \
    foo/bar3/somejs.js \
    foo/bar1/someimage.svg \
    foo/bar2/anotherjs.js \
    foo/somestyle.css \
    html/block/somepng.png

checksum:
    for f in $(FILES); do \
    echo "file: $$f"; \
    SUM=$$(md5sum $$f | cut -d' ' -f 1); \
    echo "CHECKSUM: $$SUM"; \
    done
于 2020-03-26T18:24:49.577 回答