output redirection issue - m4 macro with self increasing build counter
2
votes
1
answer
86
views
My goal is to create a m4 macro, that reads a value from a file (
BUILD
), increases it and then saves the output into the file. I came up with the following solution (BUILD.m4
):
define(__buildnumber__',
esyscmd(cat BUILD)')dnl
define(`counter',__buildnumber__)dnl
popdef(__buildnumber__)dnl
define(count',
define(`counter',eval(counter+1))counter')dnl
count dnl
When BUILD
contains 3 then running m4 BUILD.m4
outputs 4
.
Which is great!
However, when I call it like this m4 BUILD.m4 > BUILD
the BUILD
file always contains 1
.
If I pipe to another file m4 BUILD.m4 > B
it works and the B
file will contain 4
when BUILD
was 3
.
I suspect it has to do with the >
output redirection. When comparing both variants with debug tracing it seems like the one with the redirection into the same file can not read from the file anymore.
Variant redirection to different file:
% m4 -dtaeq BUILD.m4 > B
m4trace: -1- define(__buildnumber__',
esyscmd(`cat BUILD')')
m4trace: -1- dnl
m4trace: -2- __buildnumber__ -> esyscmd(
cat BUILD')'
m4trace: -2- esyscmd(cat BUILD') ->
3'
m4trace: -1- define(counter',
3')
Variant redirection to same file:
% m4 -dtaeq BUILD.m4 > BUILD
m4trace: -1- define(__buildnumber__',
esyscmd(`cat BUILD')')
m4trace: -1- dnl
m4trace: -2- __buildnumber__ -> esyscmd(
cat BUILD')'
m4trace: -2- esyscmd(`cat BUILD')
m4trace: -1- define(counter',
')
Is there a way of doing it like this, or do I need to use some other means of capturing the output
Asked by bckmnn
(23 rep)
Oct 7, 2022, 11:21 AM
Last activity: Oct 7, 2022, 02:38 PM
Last activity: Oct 7, 2022, 02:38 PM