Home > OS >  $? is ignored when run from a Makefile but works fine when copy-pasted as a bash command
$? is ignored when run from a Makefile but works fine when copy-pasted as a bash command

Time:03-01

I am trying to automatically run a command from a Makefile:

make lint: 
    pylint --rcfile=.pylintrc --disable=R,C,W1203,W0702,W0621 main.py || pylint-exit $? 

and then run the command with:

make lint

I get the following error -

usage: pylint-exit [-h] [-efail] [-wfail] [-rfail] [-cfail] PYLINTRC make: *** [Makefile:6: lint] Error 1

probably because the shell expects an integer at the end of the command, but there is none because it doesn't recognize the $? sign.

if I run the same command directly as a bash command:

pylint --rcfile=.pylintrc --disable=R,C,W1203,W0702,W0621 main.py || pylint-exit $? 

everything runs as expected:

main.py:5:0: W0611: Unused numpy imported as np (unused-import)

------------------------------------------------------------------ Your code has been rated at 9.76/10 (previous run: 9.76/10, 0.00)

The following messages were raised:

  • warning message issued

No fatal messages detected. Exiting gracefully...

Is there a workaround for this kind of behavior - I guess it is something with the way wildcard signs are handled in Makefiles.

Thanks!

CodePudding user response:

$ is both a make and shell metacharacter. You'll need to escape it so make ignores it and passes it to the shell.

pylint --rcfile=.pylintrc --disable=R,C,W1203,W0702,W0621 main.py || pylint-exit $$? 

CodePudding user response:

$? is a make parameter expansion. You want the $? to be passed literally to the shell that executes pylint-exit, by doubling the $.

make lint: 
        pylint --rcfile=.pylintrc --disable=R,C,W1203,W0702,W0621 main.py || \
          pylint-exit $$? 
  • Related