1

I'm not familiar with Makefile, and I try to run the piece of bash script in Makefile, and use the variable as result in target.

my bash script like below, read from file, merge each line to variable

#!/bin/bash

input="./logs"
context=""

while IFS= read -r var; \
do \
    context+="\n$var"; \
done < "$input"

echo -e $context

my logs file like below

- foo
- bar
- barzz

and the bash script work fine

but when I move it to Makefile, it can't works

Makefile

.PHONY: test pack clean

INPUT = "./logs"
CONTEXT = ""

while IFS= read -r var; \
do \
    CONTEXT+="\n$(var)"; \
done < "$(INPUT)"


test:
    echo -e $(CONTEXT)

how should I fix it?

thanks for your time.

L.Jovi
  • 1,631
  • 4
  • 22
  • 36

1 Answers1

1

I'm assuming the main goal here is to fill the CONTEXT variable with all the values you have in your logs file, rather than solving make running that shell line. I would keep the little shell script in it's own separate file, lets call it myScript for now. You can keep what you have for that.

Now for the makefile you can just initialize the variable by:

CONTEXT = $(shell ./myScript)

I personally think this is the cleanest way to achieve your goal. In case you really want everything in the makefile, take a look at this answer.

Community
  • 1
  • 1
super schaap
  • 128
  • 1
  • 7