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: Robert Klima Subject: Re: sh script Date: 1998/11/12 Message-ID: <364AA406.BE1E8592@iue.tuwien.ac.at>#1/1 X-Deja-AN: 411025262 Content-Transfer-Encoding: 7bit References: <36406956.3F54@g24ux.phys.uniroma1.it> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Institute for Microelectronics, Technical University Vienna Mime-Version: 1.0 Newsgroups: comp.unix.osf.misc 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/ ========================================================================