Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* sh script
@ 1998-11-04  5:00 Daniele Fua
  1998-11-04  5:00 ` Allan Hawdon
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Daniele Fua @ 1998-11-04  5:00 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 732 bytes --]

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
-- 
 ===================================================================
| Daniele Fua`  G24           | E-mail: fua@g24ux.phys.uniroma1.it  | 
| Dept. of Physics            | Phone : +39 06 4991 3515            |
| Universita` "La Sapienza"   | Fax   : +39 06 4991 3522            |
| P.le A. Moro, 2             |                                     |
| 00185 Roma, Italy           |                                     |
 ===================================================================

[-- Attachment #2: random --]
[-- Type: text/plain, Size: 233 bytes --]

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sh script
  1998-11-04  5:00 sh script Daniele Fua
  1998-11-04  5:00 ` Allan Hawdon
@ 1998-11-04  5:00 ` Daniele Fua
  1998-11-11  5:00 ` Finnbarr P. Murphy
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Daniele Fua @ 1998-11-04  5:00 UTC (permalink / raw)


sorry, by mistake I left a comment # on the second line of the script. 
Before trying it, please uncomment the line or you will get an error.
-- 
 ===================================================================
| Daniele Fua`  G24           | E-mail: fua@g24ux.phys.uniroma1.it  | 
| Dept. of Physics            | Phone : +39 06 4991 3515            |
| Universita` "La Sapienza"   | Fax   : +39 06 4991 3522            |
| P.le A. Moro, 2             |                                     |
| 00185 Roma, Italy           |                                     |
 ===================================================================



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sh script
  1998-11-04  5:00 sh script Daniele Fua
@ 1998-11-04  5:00 ` Allan Hawdon
  1998-11-04  5:00 ` Daniele Fua
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Allan Hawdon @ 1998-11-04  5:00 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sh script
  1998-11-04  5:00 sh script Daniele Fua
  1998-11-04  5:00 ` Allan Hawdon
  1998-11-04  5:00 ` Daniele Fua
@ 1998-11-11  5:00 ` Finnbarr P. Murphy
  1998-11-12  5:00 ` Robert Klima
  2000-01-26  5:00 ` Loris Renggli
  4 siblings, 0 replies; 7+ messages in thread
From: Finnbarr P. Murphy @ 1998-11-11  5:00 UTC (permalink / raw)



Try

  #! /usr/bin/posix/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}

or

  #! /usr/bin/ksh
  .....


Finnbarr



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sh script
  1998-11-04  5:00 sh script Daniele Fua
                   ` (2 preceding siblings ...)
  1998-11-11  5:00 ` Finnbarr P. Murphy
@ 1998-11-12  5:00 ` Robert Klima
  2000-01-26  5:00 ` Loris Renggli
  4 siblings, 0 replies; 7+ messages in thread
From: Robert Klima @ 1998-11-12  5:00 UTC (permalink / raw)


Hi!

The reason for this is that the 'sh' executes the 'while' command in a new shell
where another variable 'rand' is defined. Returning from this subshell, the
variable 'rand' inside the 'while-shell' is lost and the old variable 'rand'
appears again.

You might try '/bin/bash' instead of 'sh'.
In the bash a similar behavior can be observed. The bash opens a subshell too
when piping something to the while command. To avoid piping I've found a way,
which satisfys the bash: I feed it from its 'backside'. (I'm sorry for this
idiom)

Instead of
	ps -e -o pid | while read; do .... done
write
	ps -e -o pid > numbers.tmp
	while read; do ... done < numbers.tmp
This will do the job.

The whole example will then look


#! /bin/bash
rand=`expr 1`
/bin/ps -e -o pid > numbers.tmp
while read mypid
do
if [ "${mypid}" != "PID" ]; then
                rand=`expr \( "${rand}" + "${mypid}" \) % 100`
        fi
        echo "inside loop: rand=" ${rand}
done < numbers.tmp
echo "outside loop: rand=" ${rand}


Best regards,
	Robert
-- 

========================================================================
 Dipl. Ing. Robert Klima           voice : +43(1)58801-36030
 Institute for Microelectronics    fax   : +43(1)58801-36099
 Technische Universitaet Wien
 Gusshausstrasse 27-29/E360
 1040 Vienna                       email : klima@iue.tuwien.ac.at
 A U S T R I A                     www   : http://www.iue.tuwien.ac.at/
========================================================================



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sh script
  1998-11-04  5:00 sh script Daniele Fua
                   ` (3 preceding siblings ...)
  1998-11-12  5:00 ` Robert Klima
@ 2000-01-26  5:00 ` Loris Renggli
  2000-01-26  5:00   ` Daniel S. Riley
  4 siblings, 1 reply; 7+ messages in thread
From: Loris Renggli @ 2000-01-26  5:00 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sh script
  2000-01-26  5:00 ` Loris Renggli
@ 2000-01-26  5:00   ` Daniel S. Riley
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel S. Riley @ 2000-01-26  5:00 UTC (permalink / raw)


Loris Renggli <loris.renggli@sphys.unil.ch> writes:
> 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?
> >   ------------------------------------------------------------------------
> > #! /usr/bin/sh
> > #rand=`expr 1`
> > /bin/ps -e -o pid | while read mypid

The '|' forks a subshell for the while loop; variables can't get out
of the subshell back to the parent.  For this simple case, something
like

#! /bin/sh
#rand=`expr 1`
for mypid in `/bin/ps -e -o pid`
do
        if [ "${mypid}" != "PID" ]; then
                rand=`expr \( "${rand}" + "${mypid}" \) % 100`
        fi
        echo "inside loop: rand=" ${rand}
done
echo "outside loop: rand=" ${rand}

works fine, but you may have to be trickier if you really want to
process some input a line at a time.
-- 
Dan Riley                                         dsr@mail.lns.cornell.edu
Wilson Lab, Cornell University      <URL:http://www.lns.cornell.edu/~dsr/>
    "History teaches us that days like this are best spent in bed"



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2000-01-26  5:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-11-04  5:00 sh script Daniele Fua
1998-11-04  5:00 ` Allan Hawdon
1998-11-04  5:00 ` Daniele Fua
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

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