Checking file size in makefile, stopping if file is too short

百般思念 提交于 2019-12-05 13:17:04

Put this in your rule, somewhere before the compilation:

 test -n "$$(find filename -a -size +NNNc)"

where filename is the filename and NNN is the size in octets. This returns false and halts make when the size is less than or equal to NNN.

My approach for this, is probably not the most pretty solution, but it worked for me :)

CHECKFILE = \
    if [ ! -f "file" ]; then \
        echo "file does not exist" ; exit 1 ; \
    fi; \
    FSIZE=$$(du -b "file" | cut -f 1) ; \
    if [ $$FSIZE -lt 100000 ]; then \
        echo "filesize too small" ; exit 1 ; \
    fi

all:
    @$(CHECKFILE)

I prefer this-- continue if greater than 256 bytes, otherwise stop make-- for readability:

test `wc -c <$<` -gt 256;

Within the backticks, the file $< is directed into wc -c, which returns the size in bytes of $<. After the backticks evaluate the size of $<, test is then used with the -gt "greater than" operator.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!