Makefile 中的 MD5SUM

发布于 2024-11-05 12:22:23 字数 146 浏览 0 评论 0原文

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

CHECKSUM=md5sum $(myfile)

但变量 CHECKSUM 始终为空

有人能告诉我这里出了什么问题吗?

I'm trying to generate a MD5 checksum of a file in a Makefile. In my Makefile i have something like;

CHECKSUM=md5sum $(myfile)

But the variable CHECKSUM is always empty

Can anybody tell me what's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

俏︾媚 2024-11-12 12:22:23

这是一个略有不同的示例,其中我将预期的 md5 值设置为 make 变量,然后在我的配方中的 shell 命令内对其进行检查。在本例中,我想下载特定版本的 Anaconda 并在安装之前检查其 md5sum。

Makefile:

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

Here is a slightly different example, where I set the expected md5 value as a make variable, and then check against it inside the shell commands in my recipe. In this case, I wanted to download a specific version of Anaconda and check its md5sum before installing it.

Makefile:

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

Output:

$ 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

Note the usage of $$AnacondaMD5 to fill in the in-line bash variable vs. the usage of $(ANACONDA_MD5) to fill in the make variable

Version:

$ 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
旧瑾黎汐 2024-11-12 12:22:23

正如克里斯所说,您需要类似的东西:

CHECKSUM=$(md5sum $(myfile))

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

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

以下将执行您需要的操作:

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

或者,如果您需要多行,

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

如果您剪切并粘贴上面的内容,则需要插入制表符。

As Chris says, you need something like:

CHECKSUM=$(md5sum $(myfile))

In case you didn't know, CHECKSUM will only available on that line. i.e. the following will output a blank link:

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

The following will do what you need:

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

Or, if you need it over multiple lines

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

If you cut n paste the above, you need to insert tabs.

棒棒糖 2024-11-12 12:22:23

您是否需要 makefile 中命令部分之外的命令的结果?
然后,如果您的 makeGNU-make,则 $(shell) 函数可用。
例如:

CHECKSUM := $(shell md5sum $(myfile))

Do you need the result of a command outside the commands-part in makefile?
Then, if your make is GNU-make, $(shell) function is available.
For example:

CHECKSUM := $(shell md5sum $(myfile))
残龙傲雪 2024-11-12 12:22:23

这对我有用:

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..txt

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

This works for me:

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

Now run it with make md5sum.

You should get file file.<sum>.txt.

If you copy code from above, remember to use tab for indent or get the file from repo https://github.com/rofrol/makefile-md5sum.

何处潇湘 2024-11-12 12:22:23

有关文件列表,您可以在 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

For a list of files you can have following in 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文