From: Robert Klima <robert.klima@iue.tuwien.ac.at>
Subject: Re: sh script
Date: 1998/11/12 [thread overview]
Message-ID: <364AA406.BE1E8592@iue.tuwien.ac.at> (raw)
In-Reply-To: 36406956.3F54@g24ux.phys.uniroma1.it
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/
========================================================================
next prev parent reply other threads:[~1998-11-12 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 ` Allan Hawdon
1998-11-04 5:00 ` Daniele Fua
1998-11-11 5:00 ` Finnbarr P. Murphy
1998-11-12 5:00 ` Robert Klima [this message]
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