MakeFile Bash-检查压缩后的文件是否为空

时间:2019-05-05 14:22:36

标签: bash makefile

我有一个makefile,如下所示。它检查gzip文件是否为空,如果不是,则必须调用test_recipe否则打印出一条消息。但是在以下情况下,无论如何都会调用test_recipe。不知道我做错了什么,或者需要做什么来检查gzip内容。

SHELL := /bin/bash

current_month = $(shell date +"%m")
current_year = $(shell date +"%Y")
current_date = $(shell date +"%Y-%m-%d")

check_file_dump_empty :
    @ [ -s test_data.txt.gz ] && $(MAKE) test_recipe || echo "file is empty"

test_recipe :
        @bash sqoop.sh ${current_date} ${current_year}

先谢谢了。

1 个答案:

答案 0 :(得分:1)

存在gz文件但为空的文件大小将不会为零字节。您的支票错了。您需要解压缩它,然后检查是否有任何输出。

这是一个重构,它也避免了无缘无故地覆盖SHELL变量。

current_month = $(shell date +"%m")
current_year = $(shell date +"%Y")
current_date = ${current_year}-${current_month}

check_file_dump_empty:
    gzip -dc test_data.txt.gz | grep -q '^' \
    && $(MAKE) test_recipe \
    || echo "file is empty"

test_recipe:
    bash sqoop.sh "${current_date}" "${current_year}"

通常最好选择使脚本文件可执行并确保其在第一行具有正确的shebang,而不是显式调用bash

相关问题