user3273016
user3273016

Reputation: 93

Makefile: Splitting a string and looping through results

I am trying to write a target in a makefile which will read a variable(having IPs) from one of the .mk file and if a space separated list found split it and take some action.

Issue i am facing that the string do not split and in for loop do not get the value either.

Have tried following

GWTS_FE_IPS=2600:40f0:3e::2 2600:40f0:3e::3 2600:40f0:3e::4 2600:40f0:3e::5

test:
    $(eval IPS=$(shell echo "$(GW_IPS)" |awk -F " " '{print NF}'))
    if [ ${IPS} -gt 1 ]; then \
        echo "Multiple Ips [$(GW_IPS)]"; \
        for ip in $(shell echo "${GW_IPS}" | sed -e 's/ /\n/g'); \
        do \
            echo ".... $(ip) ...."; \
        done \
    else \
        echo "Single IP [$(GW_IPS)]"; \
    fi

Result i get is

2600:40f0:3e::2n2600:40f0:3e::3n2600:40f0:3e::4n2600:40f0:3e::5
if [ 4 -gt 1 ]; then \
        echo "Multiple Ips [2600:40f0:3e::2 2600:40f0:3e::3 2600:40f0:3e::4 2600:40f0:3e::5]"; \
        for ip in 2600:40f0:3e::2n2600:40f0:3e::3n2600:40f0:3e::4n2600:40f0:3e::5; \
        do \
            echo "....  ...."; \
        done \
    else \
        echo "Single IP [2600:40f0:3e::2 2600:40f0:3e::3 2600:40f0:3e::4 2600:40f0:3e::5]"; \
    fi
Multiple Ips [2600:40f0:3e::2 2600:40f0:3e::3 2600:40f0:3e::4 2600:40f0:3e::5]
....  ....

Can any one give some pointers.

Upvotes: 0

Views: 1957

Answers (1)

Beta
Beta

Reputation: 99094

You are trying to do too many things at once without testing any of them. When you try new tools, try them one at a time.

GWTS_FE_IPS=2600:40f0:3e::2 2600:40f0:3e::3 2600:40f0:3e::4 2600:40f0:3e::5

IPS := $(words $(GWTS_FE_IPS))

test:
    @if [ ${IPS} -gt 1 ]; then \
  echo "Multiple Ips [$(GW_IPS)]"; \
  for ip in $(GWTS_FE_IPS) ; \
    do \
      echo ".... $$ip ...."; \
    done \
  else \
    echo "Single IP [$(GW_IPS)]"; \
fi

Upvotes: 1

Related Questions