From: John Kelly on 11 Aug 2010 19:59 On Wed, 11 Aug 2010 23:56:08 +0100, Ben Bacarisse <ben.usenet(a)bsb.me.uk> wrote: >John Kelly <jak(a)isp2dial.com> writes: > >> On Wed, 11 Aug 2010 16:19:03 -0400, Wayne <nospan(a)all.invalid> wrote: ><snip> >>>rand() >>>{ >>> awk 'BEGIN {srand();printf "%d\n", (rand() * 10^8);}' >>>} ><snip> >> That's interesting. But it looks like it calls awk every time it wants >> a new random number. I want to get new random numbers without spawning >> an extra subshell for each one. > >Weren't you suggesting hexdump a few hours ago? It's not a new subshell >but then nether is awk (usually). I meant a another forked pid to run it. "Subshell" was the wrong word to use. Yes hexdump will require that too. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: John Kelly on 11 Aug 2010 20:08 On Wed, 11 Aug 2010 19:22:04 -0400, Wayne <nospan(a)all.invalid> wrote: >The reason for the awk is that this is the only way (that I know of) >in POSIX to get a random number, without writing C code. And even >then the standard is silent on the type and strength of the resulting >random numbers. I suppose it would be possible to whip up a crude >RNG as a shell function, using only shell math. Should be doable. > But I doubt that would be more efficient than using awk. The math won't be, but I want to avoid forking a new pid for each random number. >Of course, if POSIX (SUS) portability doesn't matter, there certainly >are easier ways. Also I imagine the source for Gnu mktemp is available >for download, I doubt you need all of Gnu core utils package just for >that one utility. I could use C and write a loadable shell builtin, but then it's not standard. I was looking for shell code that will run on most shells. I thought maybe somebody had already done that. If not, never mind. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: Janis Papanagnou on 12 Aug 2010 03:02 On 12/08/10 01:22, Wayne wrote: > On 8/11/2010 4:50 PM, John Kelly wrote: >> On Wed, 11 Aug 2010 16:19:03 -0400, Wayne <nospan(a)all.invalid> wrote: >> >>> Maybe you could adapt this script to produce directories instead: >>> >>> # mktemp.sh - A portable and compliant mktemp replacement. >>> # Usage: mktemp.sh [ base_file_name ] >>> # >>> # (See also the POSIX definition of pathchk utility, the >>> # rational section, for examples and a discussion of this.) >>> >>> set -C # turn on noclobber shell option >>> >>> rand() >>> { >>> awk 'BEGIN {srand();printf "%d\n", (rand() * 10^8);}' >>> } >>> >>> umask 177 >>> >>> NAME="$1" >>> NAME="${NAME:=tmp}" >>> >>> while : >>> do >>> TMP=${TMPDIR:-/tmp}/$NAME-$$.$(rand) >>> # pathchk -p $TMP && > $TMP && break >>> : > $TMP && break >>> done >>> >>> printf "%s\n" "$TMP" >>> >>> >>> Hope that helps. >> >> That's interesting. But it looks like it calls awk every time it wants >> a new random number. I want to get new random numbers without spawning >> an extra subshell for each one. > > The reason for the awk is that this is the only way (that I know of) > in POSIX to get a random number, without writing C code. And even > then the standard is silent on the type and strength of the resulting > random numbers. I suppose it would be possible to whip up a crude > RNG as a shell function, using only shell math. But I doubt that would > be more efficient than using awk. I would certainly think that it would be a lot more efficient if you do it in shell, since in the example above you would have to call one new process in every new loop iteration. To be efficient it would be better to set up a co-process if using awk or any other external program. Janis > [...]
From: Jon LaBadie on 12 Aug 2010 11:00 John Kelly wrote: > On Wed, 11 Aug 2010 19:22:04 -0400, Wayne <nospan(a)all.invalid> wrote: > >> The reason for the awk is that this is the only way (that I know of) >> in POSIX to get a random number, without writing C code. And even >> then the standard is silent on the type and strength of the resulting >> random numbers. I suppose it would be possible to whip up a crude >> RNG as a shell function, using only shell math. > > Should be doable. > > >> But I doubt that would be more efficient than using awk. > > The math won't be, but I want to avoid forking a new pid for each random > number. > > >> Of course, if POSIX (SUS) portability doesn't matter, there certainly >> are easier ways. Also I imagine the source for Gnu mktemp is available >> for download, I doubt you need all of Gnu core utils package just for >> that one utility. > > I could use C and write a loadable shell builtin, but then it's not > standard. I was looking for shell code that will run on most shells. > > I thought maybe somebody had already done that. If not, never mind. > > It is not POSIX, but ksh, bash, and zsh (probably others too) all define a RANDOM variable. Its range is 0-32K.
First
|
Prev
|
Pages: 1 2 3 Prev: sed and quoting issue Next: ssh and passing second arguments through awk |