Search code examples
m4

In m4, how do you include a file that has an environment variable in its name?


I want to include a file based relative to my sandbox base directory inside of my m4 text without using the -I switch.

So far, I have figured out how to grab the environment variables using a sys call:

define(MODEL_ROOT,`syscmd(`printf $MODEL_ROOT')')dnl

Next, I want to include a file based off that environment variable:

include(MODEL_ROOT/sw/lib/m4_macros/foreach2.m4)

In total, I have:

define(MODEL_ROOT,`syscmd(`printf $MODEL_ROOT')')

MODEL_ROOT

MODEL_ROOT/sw/lib/m4_macros/foreach2.m4

include(MODEL_ROOT/sw/lib/m4_macros/foreach2.m4)

Which prints:

/home/ross/sandbox

/home/ross/sandbox/sw/lib/m4_macros/foreach2.m4

/home/ross/sandboxforeach_example.m4:7: m4: Cannot open /sw/lib/m4_macros/foreach2.m4: No such file or directory

I know that the normal syntax for includes is

include(`file.m4')

But if I quote MODEL_ROOT/sw/lib/m4_macros/foreach2.m4, then m4 like:

[...]
include(`MODEL_ROOT/sw/lib/m4_macros/foreach2.m4')

m4 complains:

[...]
foreach_example.m4:7: m4: Cannot open MODEL_ROOT/sw/lib/m4_macros/foreach2.m4: No such file or directory

How does one include a file with an environment variable in its path?


Solution

  • I think you need to use esyscmd instead of syscmd. esyscmd reads command line output.