Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: udaa460@newt.kcl.ac.uk (Allan Hawdon)
Subject: Re: sh script
Date: 1998/11/04	[thread overview]
Message-ID: <71pv9u$li5$1@willow.cc.kcl.ac.uk> (raw)
In-Reply-To: 36406956.3F54@g24ux.phys.uniroma1.it

Daniele Fua (fua@g24ux.phys.uniroma1.it) wrote:
: This is a multi-part message in MIME format.

<snip>

: 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
: -- 
 <snip>

: #! /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



  parent reply	other threads:[~1998-11-04  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 [this message]
1998-11-11  5:00 ` Finnbarr P. Murphy
1998-11-12  5:00 ` Robert Klima
2000-01-26  5:00 ` Loris Renggli
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