TPPZ
TPPZ

Reputation: 4901

Makefile evaluate 2 variables then use the non-empty one

I have a TV box running CoreELEC (a Linux distro similar to LibreELEC) and I would like to find its IP address to then use SSH to log into it.

I know the MAC addresses of both the Ethernet interface and the Wifi interface.

I want to find the IP address of my TV box in my LAN (either Ethernet or Wifi) assuming I don't know which interface is currently used by the TV box to connect to the LAN.

The following list of targets does not work as it fails when evaluating the "OR":

TVBOX_IP=""

find-tvbox-eth-ip:
    @echo Checking the Ethernet IP address...
    $(eval TVBOX_ETH_IP=$(shell sudo arp-scan 192.168.0.1/24 2>&1 | grep $(TV_ETH_MAC) | awk '{print $$1}'))
    @echo Found an Ethernet IP address: "$(TVBOX_ETH_IP)"

find-tvbox-wifi-ip:
    @echo Checking the Wifi IP address...
    $(eval TVBOX_WIFI_IP=$(shell sudo arp-scan 192.168.0.1/24 2>&1 | grep $(TV_WLAN_MAC) | awk '{print $$1}'))
    @echo Found a Wifi IP address: "$(TVBOX_WIFI_IP)"

find-tvbox-ip: find-tvbox-eth-ip find-tvbox-wifi-ip
    @[ "$(TVBOX_ETH_IP)" ] && TVBOX_IP:=$(TVBOX_ETH_IP) || ( echo TVBOX_IP:=$(TVBOX_WIFI_IP) )
    @echo "Stored this IP for the TV BOX: " $(TVBOX_IP)

A similar target containing TVBOX_IP := $(or $(TVBOX_ETH_IP),$(TVBOX_WIFI_IP)) does not work as well.

Upvotes: 0

Views: 75

Answers (1)

dash-o
dash-o

Reputation: 14468

Sometimes, it is easier to move logic to external commands. Make declarative approach is sometimes hard to control:

Consider the alternative: Moving the conditional to the shell.

  # Notice this is definition, will not force immediate evaluation
TVBOX_ETH_IP= sudo arp-scan 192.168.0.1/24 2>&1 | grep $(TV_ETH_MAC) | awk '{print $$1}'
TVBOX_WIFI_IP=sudo arp-scan 192.168.0.1/24 2>&1 | grep $(TV_WLAN_MAC) | awk '{print $$1}'

find-tvbox-ip:
    IP=$$(${TVBOX_ETH_IP}) ; [ "$$IP" ] || IP=$$(${TVBOX_WIFI_IP}) ; echo "IP=$$IP"

Or keeping the condition in make $(or ...)

TVBOX_ETH_IP=$(shell sudo arp-scan 192.168.0.1/24 2>&1 | grep $(TV_ETH_MAC) | awk '{print $$1}')
TVBOX_WIFI_IP=$(shell sudo arp-scan 192.168.0.1/24 2>&1 | grep $(TV_WLAN_MAC) | awk '{print $$1})'

TVBOX_IP=$(or ${TVBOX_ETH_IP}, ${TVBOX_ETH_IP})

find-tvbox-ip:
    IP=${TVBOX_IP} ; echo "IP=$$IP"

Upvotes: 1

Related Questions