简体   繁体   中英

Assign variable read from file in Makefile recipe

I am trying to do the following in a Makefile recipe. Get the server container-ip using a python script. Build the command to run within the docker container. Run the command in the docker container.

test:
    SIP=$(shell python ./scripts/script.py get-server-ip)
    CMD="iperf3 -c ${SIP} -p 33445"
    docker exec server ${CMD}

I get this

$ make test
SIP=172.17.0.6
CMD="iperf3 -c  -p 33445"
docker exec server 
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container make: *** [test] Error 1

I ended up with something like this.

SERVER_IP=$(shell python ./scripts/script.py get-server-ip); \
    SERVER_CMD="iperf3 -s -p ${PORT} -4 --logfile s.out"; \
    CLIENT_CMD="iperf3 -c $${SERVER_IP} -p ${PORT} -t 1000 -4 --logfile c.out"; \
    echo "Server Command: " $${SERVER_CMD}; \
    echo "Client Command: " $${CLIENT_CMD}; \
    docker exec -d server $${SERVER_CMD}; \
    docker exec -d client $${CLIENT_CMD};

This seems to work ok. Would love to hear if there are other ways of doing this.

You could write something like this. Here I used a target-specific variable assuming the IP address is required only in this rule. iperf_command is defined as a variable as the format looks rather fixed except the IP address which is injected by call function .

Also, as the rule doesn't seem to be supposed to produce the target as a file, I put .PHONY target as well.

iperf_command = iperf3 -c $1 -p 33445

.PHONY: test
test: iperf_server_ip = $(shell python ./scripts/script.py get-server-ip)
test:
    docker exec server $(call iperf_command,$(iperf_server_ip))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM