I have a makefile as below . It checks if the gzip file is empty , if not it must called the test_recipe else print out a message . But in the below case , it calls the test_recipe anyway. Not sure what im doing wrong or what needs to be done to check the gzip content .

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}

Thanks in advance .

1 Answers

1
tripleee On Best Solutions

A gz file which exists but is empty will not be zero bytes in size. Your check is wrong. You'll want to decompress it and check whether there is any output.

Here's a refactoring which also avoids overriding the SHELL variable for no good reason.

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}"

Instead of explicitly calling bash, making the script file executable and ensuring that it has a correct shebang on the first line is usually preferable.