Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: Loris Renggli <loris.renggli@sphys.unil.ch>
Subject: Re: sh script
Date: 2000/01/26	[thread overview]
Message-ID: <388F2BE4.F2B81A0F@sphys.unil.ch> (raw)
In-Reply-To: 36406956.3F54@g24ux.phys.uniroma1.it

Daniele Fua wrote:
> 
> Anyone can suggest why the attached small sh script for pseudo random
> number generation [0-99] is not able to keep the variable $rand outside
> the scope of the while loop? Any suggested fix?
> My operating system is Digital UNIX V4.0D
> Thanks
> --
> 
>   ------------------------------------------------------------------------
> #! /usr/bin/sh
> #rand=`expr 1`
> /bin/ps -e -o pid | while read mypid
> do
>         if [ "${mypid}" != "PID" ]; then
>                 rand=`expr \( "${rand}" + "${mypid}" \) % 100`
>         fi
>         echo "inside loop: rand=" ${rand}
> done
> echo "outside loop: rand=" ${rand}

Did you find a solution ?

What about:

#!/bin/sh

rand=1

set - `/bin/ps -e -o pid`
while [ $# -gt 0 ]
do
mypid=$1
      if [ "$mypid" != "PID" ]; then
              rand=`expr \( $rand + $mypid \) % 100`
      fi
      echo "inside loop: rand=" $rand
shift
done
echo "outside loop: rand=" $rand


-- 
-------------------------------------------------------------------
Loris Renggli,   BSP,   Universite de Lausanne,    CH-1015 Lausanne
              e-mail: loris.renggli@sphys.unil.ch
tel: +41-21-692 3603                           fax: +41-21-692 3605



  parent reply	other threads:[~2000-01-26  5:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-04  5:00 sh script Daniele Fua
1998-11-04  5:00 ` Daniele Fua
1998-11-04  5:00 ` Allan Hawdon
1998-11-11  5:00 ` Finnbarr P. Murphy
1998-11-12  5:00 ` Robert Klima
2000-01-26  5:00 ` Loris Renggli [this message]
2000-01-26  5:00   ` Daniel S. Riley
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox