[please don't top post on technical lists]

On 08/18/2010 11:44 AM, mikeyzman wrote:
> 
> Thanks for the response
> 
> I put in a $SHELL call so you can see what is going on:
> esyscmd(`printf $SHELL')dnl
> esyscmd(`printf $USER')dnl
> esyscmd(`printf $RANDOM')dnl
> 
> yields:
> 
> /bin/bashdnl

Well, yeah - $SHELL may contain the string bash, but that doesn't impact
whether /bin/sh is something else like dash.  Dash doesn't change $SHELL
if it was inherited from the environment.

> mikezdnl
> printf: 1: usage: printf format [arg ...]

And dash's /bin/sh does NOT have $RANDOM, so you ended up doing the same
as esyscmd(`printf ').

> 
> 
> So I tried your suggestion, standalone (esyscmd([/bin/bash -c 'printf
> "$RANDOM"']))
> 
> and here is my output:
> 
> include(`m4include1')
> sh: [/bin/bash: not found
> m4: esyscmd subprocess failed

Well, that's because you aren't using consistent quoting.

By default, m4 uses `' quoting.  But if you use changequote([,]), it
uses [] quoting.  Given that the original example in your first message
used [] quoting, I assumed that you already knew about changequote:

>> dnl random(num): Evaluates to a random number from range 0..num-1.
>> define([random],[eval(esyscmd(printf $RANDOM) % $1)])dnl

But if you are intending to stick with `' quoting, then use:

esyscmd(`/bin/bash -c "\"$RANDOM\""')

By the way, since mixing `' and shell code is rather painful, that is an
argument for using changequote.

> Im running M4 on Ubuntu, I don't know if that matters

It does matter.  Ubuntu defaults /bin/sh to dash.  Fedora, on the other
hand, leaves /bin/sh as bash.

-- 
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to