From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f678f,215796c423b2640b X-Google-Attributes: gidf678f,public From: udaa460@newt.kcl.ac.uk (Allan Hawdon) Subject: Re: sh script Date: 1998/11/04 Message-ID: <71pv9u$li5$1@willow.cc.kcl.ac.uk>#1/1 X-Deja-AN: 408337375 References: <36406956.3F54@g24ux.phys.uniroma1.it> Organization: King's College London Newsgroups: comp.unix.osf.misc Daniele Fua (fua@g24ux.phys.uniroma1.it) wrote: : This is a multi-part message in MIME format. : 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} I think you're getting caught by the pipe which is giving you a new process. You should be able to get round it with a backticks assignment on the ps command and a for loop in place of the while. There's probably even a neater way, but I'm not feeling very creative today. Allan