Re: random number generator

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Organization Red Hat
Message-ID <[email protected]>
[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
signature.asc (application/pgp-signature, 619 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iQEcBAEBCAAGBQJMbB8uAAoJEKeha0olJ0Nqia4H/jmLnS8LQkb/WMf+nRYPCFWq
2v9oz2rXWC9J5cQaZw0lRfupRgovDdNYPE/5owZUeQeZa9LX+iJbkqHfp7Xbg502
zqdyBZAeVLTHxoh0Cd4QKxCfUlW7PKv4ntKP06Vv64+luO8Y4cZP3/i+KAZae7Bl
QPAkJJ/svl5hL7luaN1WZ/5cKmboG9LH3zXpvLuLYVyo1WEOEXZezETxEmtUgsDN
qdZpliZeABkdr30oeBQmBV+xwVbzJyfn2sZUkhJbtakJqlvBhBuLYH2C+4Ry5fD1
jQz+DxqhCQgfuJtZYXHOd2e7bIcI+5C38+VLJANOmYS/H8trBW7Q++Xy/P9sVO0=
=634Z
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.