0

I have the following GNU make Makefile


PWD := $(shell pwd)

obj-m += liason.o

default:
        bash -c '[ "$(lsmod|grep liason)" == "" ] || rmmod liason'
        make -C /src M=$(PWD) modules

I invoke the makefile; it shows this among the output:

bash -c '[ "" == "" ] ||  rmmod liason'

Thus, rmmod isn't executed.

However:

Module "liason" is installed.
It even shows up when I execute the following from cmdline.

$ lsmod|grep liason
liason                 16384  0

Therefore, empty quotes for the same lsmod|grep in the makefile output doesn't make sense.

1 Answer 1

3

The $ is being processed by Make; $(lsmod|grep liason) produces an empty string, the commands aren’t actually executed. To pass the expression on to the shell instead, you need to double the $:

bash -c '[ "$$(lsmod|grep liason)" == "" ] || rmmod liason'

You can simplify this as follows:

if lsmod | grep -q liason; then rmmod liason; fi

This ensures that any error when removing the module will stop the build.

7
  • The simplified version is producing rather inscrutable error, "make: *** [Makefile:3: all] Error 1"
    – clearlight
    Oct 7, 2021 at 3:59
  • appending "; true" to each line to throw away the exit status of 1 from the lsmod | grep -q works.
    – clearlight
    Oct 7, 2021 at 4:13
  • 1
    Ah yes, sorry! Appending ; true means that any rmmod error will be ignored, see the updated answer for an alternative. Oct 7, 2021 at 6:47
  • 1
    My point is that when rmmod is supposed to be executed, if the removal fails, it will exit with a non-zero exit code. Adding ; true will ignore that, as well as the exit code from grep when rmmod isn’t supposed to be run. Ignoring grep is fine because it’s expected; ignoring rmmod isn’t. Wrapping grep in if handles the grep error without ignoring rmmod’s potential errors. Oct 7, 2021 at 16:12
  • 1
    Yes, I understand, I only thought it worth clarifying my reasoning. Oct 7, 2021 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .