m4-discuss
[Top][All Lists]
Advanced

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

Re: random number generator


From: Eric Blake
Subject: Re: random number generator
Date: Wed, 18 Aug 2010 11:58:05 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.7) Gecko/20100720 Fedora/3.1.1-1.fc13 Lightning/1.0b2pre Mnenhy/0.8.3 Thunderbird/3.1.1

[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   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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