0

i'd like to write a Makefile (gnu make) like the following i.e. for a given list of values stored in a variable, i'd like to run a command. Is it possible or do i have to write an ugly for loop?

The ugly rules works but the smart does not.

SERVERS:=localhost
COMMAND:=uptime
OUTPUTS:=$(patsubst %, %.output ,$(SERVERS))

.PHONY: smart ugly

smart: $(OUTPUTS)

%.output: %
        ssh -n $^ $(COMMAND) > $@ || $(RM) $@

ugly:
        for s in $(SERVERS); do \
                ssh -n $$s $(COMMAND) > $$s.output || $(RM) $$s.output; \
        done

1 Answer 1

1

The smart rule looks for a prerequisite file % (localhost in your example), which is not present. The rule is skipped accordingly.

So, use:

%.output:

instead and it should work.

Also, you might have a look at the special .DELETEONERROR target instead of doing || rm $@. In your notation, a failed SSH still exits with exit code 0 (from the rm) and the whole make process will exit with success or (when %.output is a dependency) happily go on after failure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.