On Sat, Nov 19, 2022 at 09:42:07PM -0500, David Niklas wrote: > Hello, > > I know I could do this with bash, awk, and probably several other > languages, I wanted to use m4. > > I was trying to do a basic "If the file is there then include it," > directive with m4. > > echo 'define(`mkv'\'', `ogg'\'')ifelse(`1'\'', syscmd(`test -f > f'\'')sysval, `-i'\'' f `-map 1'\'')dnl' | m4 -D f="$i" -
You are testing whether 'f' exists, rather than whether the filename contained in macro f exists. You probably want your m4 script to say something like: syscmd(`test -f 'defn(`f')) plus the extra quoting needed to write ' in shell. > > As a bit of a bonus question, can I somehow tack a period onto the front > of the mkv macro expansion? That way, instead of matching "mkv", it would > match ".mkv". If you are asking "can m4 treat '.mkv' as a macro name", the answer is no for m4 1.4 unless it was built with --enable-changeword (which penalizes speed performance). But if you are asking if you can write an m4 macro that inspects whether its argument includes the substring '.mkv' and if so does something, then yes. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org