Discussion:
assign file text to variable within makefile
Add Reply
alex
2024-12-23 11:09:15 UTC
Reply
Permalink
$ tree
├── makefile
├── snippet.txt
└── template.txt

$ cat makefile
test:
sh -c "input=$(cat snippet.txt) envsubst < template.txt > output2.txt"

$ cat snippet.txt
xxx
yyy
zzz

$ cat template.txt
START
$input
END

$ input=$(cat snippet.txt) envsubst < template.txt > output1.txt

$ make
sh -c "input= envsubst < template.txt > output2.txt"

I point out that after "input=" maybe something is missing

Anyway let's check the outputs

$ cat output1.txt
START
xxx
yyy
zzz
END

OK

$ cat output2.txt
START

END

As you can see it's missing
Why?
Dan Purgert
2024-12-23 11:15:06 UTC
Reply
Permalink
Post by alex
$ input=$(cat snippet.txt) envsubst < template.txt > output1.txt
(snip working as expected)
Post by alex
$ make
sh -c "input= envsubst < template.txt > output2.txt"
[...]
$ cat output2.txt
START
END
As you can see it's missing
Why?
Because you have "input= " (i.e. "input is NUL") coming out of the
makefile.
--
|_|O|_|
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860
alex
2024-12-23 18:50:36 UTC
Reply
Permalink
Post by Dan Purgert
Because you have "input= " (i.e. "input is NUL") coming out of the
makefile.
and so a possible solution?
Dan Purgert
2024-12-23 19:50:23 UTC
Reply
Permalink
Post by alex
Post by Dan Purgert
Because you have "input= " (i.e. "input is NUL") coming out of the
makefile.
and so a possible solution?
Use makefile variables, rather than shell commands. Or maybe do that
stuff in a script rather than a makefile.

Not 100% sure, really, I don't use make for much other than triggering
compilation or uploading completed work to a microcontroller.
--
|_|O|_|
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860
alex
2024-12-24 09:37:05 UTC
Reply
Permalink
Post by Dan Purgert
uploading completed work to a microcontroller.
Meaning what?
Dan Purgert
2024-12-24 17:11:54 UTC
Reply
Permalink
Post by alex
Post by Dan Purgert
uploading completed work to a microcontroller.
Meaning what?
Meaning exactly what I said. It invokes the necessary command(s) to
kick the compiled program(s) to a target device.
--
|_|O|_|
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860
alex
2025-01-07 11:38:56 UTC
Reply
Permalink
Post by Dan Purgert
Post by alex
Post by Dan Purgert
uploading completed work to a microcontroller.
Meaning what?
Meaning exactly what I said. It invokes the necessary command(s) to
kick the compiled program(s) to a target device.
Sorry for the absence, i took a Christmas break.

Basically something like this

rsync -ai -delete SRC DEST

?
alex
2025-01-13 17:02:02 UTC
Reply
Permalink
Post by alex
Post by alex
Post by Dan Purgert
uploading completed work to a microcontroller.
Meaning what?
Meaning exactly what I said.  It invokes the necessary command(s) to
kick the compiled program(s) to a target device.
Sorry for the absence, i took a Christmas break.
Basically something like this
rsync -ai -delete SRC DEST
?
is there anyone?
Dan Purgert
2025-01-13 19:37:06 UTC
Reply
Permalink
Post by alex
Post by Dan Purgert
Post by alex
Post by Dan Purgert
uploading completed work to a microcontroller.
Meaning what?
Meaning exactly what I said. It invokes the necessary command(s) to
kick the compiled program(s) to a target device.
Sorry for the absence, i took a Christmas break.
Basically something like this
rsync -ai -delete SRC DEST
No, it launches whatever program is necessary for flashing the
microcontroller or EEPROM, etc. (usually something like minipro or
avrdude, or whatever nonsense the RP2040 needs)
--
|_|O|_|
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860
Lem Novantotto
2024-12-23 23:39:39 UTC
Reply
Permalink
Post by alex
sh -c "input=$(cat snippet.txt) envsubst < template.txt > output2.txt"
sh -c 'input=`cat snippet.txt` envsubst < template.txt > output2.txt'
--
Bye, Lem
alex
2024-12-24 09:33:45 UTC
Reply
Permalink
Post by Lem Novantotto
Post by alex
sh -c "input=$(cat snippet.txt) envsubst < template.txt > output2.txt"
sh -c 'input=`cat snippet.txt` envsubst < template.txt > output2.txt'
$ cat snippet.txt
"autoload": {
"psr-4": {
"aaa\\bbb\\": "src"
}
},
"require": {
"ccc/ddd": "^1.0",
}

$ sh -c "autoload=`cat snippet.txt`"
sh: 1: autoload:: not found
sh: 2: psr-4:: not found
sh: 3: aaa\bbb\:: not found
sh: 4: Syntax error: "}" unexpected

How many stories for a trivial problem :(
Lem Novantotto
2024-12-24 10:06:21 UTC
Reply
Permalink
Post by alex
$ cat snippet.txt
"autoload": {
"psr-4": {
"aaa\\bbb\\": "src"
}
},
"require": {
"ccc/ddd": "^1.0",
}
$ sh -c "autoload=`cat snippet.txt`"
$ sh -c 'autoload=`cat snippet.txt` && echo "$autoload"'
"autoload": {
"psr-4": {
"aaa\bbb\": "src"
}
},
"require": {
"ccc/ddd": "^1.0",
}
--
Bye, Lem
alex
2025-01-07 13:17:46 UTC
Reply
Permalink
...
Sorry for the absence, i took a Christmas break.

In summary, you have to use single quotes.
So, the substitution
(https://en.wikipedia.org/wiki/Command_substitution) is done by the sub
shell, and not by the shell

sh -c "..." # ERROR
sh -c '...' # OK

$ sh -c "echo `cat snippet.txt`"
autoload: {
sh: 2: psr-4:: not found
sh: 3: aaa\bbb\:: not found
sh: 4: Syntax error: "}" unexpected

$ sh -c 'echo "`cat snippet.txt`"'
"autoload": {
"psr-4": {
"aaa\bbb\": "src"
}
},
"require": {
"ccc/ddd": "^1.0",
}
Loading...