Home > Software design >  .SHELLSTATUS empty when echo in make recipe
.SHELLSTATUS empty when echo in make recipe

Time:06-30

I'm trying to error in a Makefile based on the the exitcode of a python script. The catch is that I still want the print output of that script, even if it is either succesfull and failed.
In short I need to replace a hashed password which is returned by the python script into a yaml config file.
I got the following, but I am unable to parse .SHELLSTATUS into the if statement. I'm using version GNU Make 4.3
The Makefile

HASHED_SECRET = $(shell python3 $(DIR)/src/generate_secret.py $(PASSWORD))

_config:
    $(info $(HASHED_SECRET))
    $(info $(.SHELLSTATUS))
    @if [ "$$.SHELLSTATUS" != 0 ]; then \
        echo "error: $$HASHED_SECRET; exit 1;\
    fi

And generate_secret.py

"""Return hash of the key, used for prometheus web-portal access configuration"""

import sys
try:
    import bcrypt
except ImportError as e:
    print(e)
    sys.exit(1)
# pylint: disable=line-too-long

args = sys.argv

if len(args) == 2:
    try:
        PASSWORD = str(args[1])
        hashed_password = bcrypt.hashpw(PASSWORD.encode("utf-8"), bcrypt.gensalt())
        print(hashed_password.decode())
        sys.exit(0)
    except BaseExceptionas e:
        print(e)
        sys.exit(1)

else:
    print('not enough arguments given')
    sys.exit(1)

CodePudding user response:

I am unable to parse .SHELLSTATUS into the if statement.

This ...

    @if [ "$$.SHELLSTATUS" != 0 ]; then \

... instructs the shell to test the value of a shell variable named .SHELLSTATUS. What you want to access is a make variable by that name (which isn't even valid as a shell variable name, on account of the . in it). You may use either ${.SHELLSTATUS} or $(.SHELLSTATUS) to do that, but I prefer the latter syntax because it is harder to confuse that with a shell variable access. The same applies to make variable HASHED_SECRET.

Overall, then:

_config:
    $(info $(HASHED_SECRET))
    $(info $(.SHELLSTATUS))
    @if [ "$(.SHELLSTATUS)" != 0 ]; then \
        echo 'error: $(HASHED_SECRET)'; exit 1;\
    fi

Note also that I have both inserted the missing quotation mark and converted from double quotes to apostrophes for HASHED_SECRET. That will not interfere with make expanding the value, and it is safer (but not completely safe) because the shell will not recognize any special characters within, except only another apostrophe.

  • Related