m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: m4 newbie q: ifelse not operating based on the value of sysval


From: deference
Subject: Re: m4 newbie q: ifelse not operating based on the value of sysval
Date: Mon, 5 Dec 2022 14:33:12 -0500

Oops! I hit reply and it went to Erik. Sorry!

On Tue, 29 Nov 2022 08:21:47 -0600
Eric Blake <eblake@redhat.com> wrote:
> 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'))

Sorry for taking so long to get back to you.
The new command is:

for i in *.mkv; do  echo 'define(`mkv'\'', `ogg'\'')ifelse(`1'\'',
syscmd(`test -f '\''defn(`f'\''))sysval, `-i'\'' f `-map 1'\'')dnl' | m4
-D f="$i" -; done

I get the following output:
sh: 1: test: MY-FILE: unexpected operator
...

I'm not really sure what's going on. The filenames do need to be quoted,
but if I replace the test command with the echo command I get the
expected string output.
-f MY-FILE 01.ogg
-f MY-FILE 02.ogg
...

> plus the extra quoting needed to write ' in shell.
>

I think I got that, see above.

> > 
> > 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.
> 

I'd like to have a "macro that inspects whether its argument includes the
substring '.mkv' and if so does something."

Thanks!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]