From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f678f,956a24d31e0300cf,start X-Google-Attributes: gidf678f,public From: Loris Renggli Subject: Re: sh script Date: 2000/01/26 Message-ID: <388F2BE4.F2B81A0F@sphys.unil.ch>#1/1 X-Deja-AN: 577912081 Content-Transfer-Encoding: 7bit References: <36406956.3F54@g24ux.phys.uniroma1.it> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Trace: 26 Jan 2000 18:16:20 +0100, ic1.unil.ch MIME-Version: 1.0 Newsgroups: comp.unix.osf.misc 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