Conditional execution using ifneq

Conditional execution can also be done using ifneq which is the opposite of ifeq.

The syntax of ifneq is

ifneq(,) statements else statement endif

ifneq(,) returns true when and are not equal. If and are equal it jumps to the else statement.

Using the above logic, the example used in the post “Conditional execution using ifeq” can be re written using ifneq as below.

CC = cc -o pf=hello user = $(shell whoami) ifneq ($(user),root) targets := $(pf)_1 else targets = $(pf)_2 endif $(targets):$(targets).c $(CC) $(targets) $(targets).c

Thus ifneq checks if the current user is not root, which if true then sets the target to $(pf)_1, else it sets it to $(pf)_2.

If we execute the make command as root we will get the following output

$ make cc -o hello_2 hello_2.c

If we do the same as any other user other than root, we will get the following output

$ make cc -o hello_2 hello_2.c


Tags: , ,
Copyright 2017. All rights reserved.

Posted November 26, 2012 by Tux Think in category "Linux