Subject: Re: how to replace $RANDOM
To: Jeremy C. Reed <reed@reedmedia.net>
From: Louis Guillaume <lguillaume@berklee.edu>
List: netbsd-users
Date: 03/21/2007 21:47:08
Jeremy C. Reed wrote:
>> How can I get the same behaviour as $RANDOM in a portable way?
> 
> I saw another example in pkgsrc that just uses $$.
> 
> Here are some ideas:
> 
> http://home.comcast.net/~j.p.h/cus-faq.html#2
> 
> http://linuxgazette.net/issue55/tag/4.html
> 
> http://www.shelldorado.com/scripts/cmds/rand
> 
> Also jot -r does random but that is not portable.
> 
> And our NetBSD ksh has $RANDOM also.


Thanks. I think that using $$ is appropriate here. The patch below fixes
complaints from pkgsrc about portability in subversion-base. $RANDOM was
only used to choose a random port number, so I used (( $$ % 32768 )) in
its place. I'm going to post the patch on the Subversion-users mailing
list and see if anyone says anything. Otherwise we could maybe put the
patch in pkgsrc??

Louis



--- subversion/tests/cmdline/davautocheck.sh.orig       2007-03-21
22:01:10.000000000 +0000
+++ subversion/tests/cmdline/davautocheck.sh    2007-03-21
22:24:01.000000000 +0000
@@ -62,7 +62,7 @@
   echo -n "$SCRIPT: $1 (y/n)? [$2] "
   read -n 1 -t 32
   echo
-  [ "${REPLY:-$2}" == 'y' ]
+  [ "${REPLY:-$2}" = 'y' ]
 }

 function get_loadmodule_config() {
@@ -170,7 +170,7 @@
     || fail "Authz_User module not found."
 }

-HTTPD_PORT=$(($RANDOM+1024))
+HTTPD_PORT=$((($$ % 32768 ) +1024))
 HTTPD_ROOT="$ABS_BUILDDIR/subversion/tests/cmdline/httpd-$(date
'+%Y%m%d-%H%M%S')"
 HTTPD_CFG="$HTTPD_ROOT/cfg"
 HTTPD_PID="$HTTPD_ROOT/pid"
@@ -268,7 +268,7 @@

 say "HTTPD is good, starting the tests..."

-if [ $# == 0 ]; then
+if [ $# = 0 ]; then
   time make check "BASE_URL=$BASE_URL"
   r=$?
 else