Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!sunybcs!boulder!hao!oddjob!gargoyle!ihnp4!cuae2!ltuxa!ttrdc!ttrdd!mellman From: mellman@ttrdd.UUCP Newsgroups: comp.unix.questions Subject: variables set in sh while loop don't get set? Message-ID: <212@ttrdd.UUCP> Date: Thu, 24-Sep-87 11:22:46 EDT Article-I.D.: ttrdd.212 Posted: Thu Sep 24 11:22:46 1987 Date-Received: Sun, 27-Sep-87 22:04:08 EDT Organization: AT&T, Skokie, IL Lines: 81 Keywords: sh, ksh scripts, loops When I set a variable in a while loop in a sh script, in some scripts it doesn't seem to get set. For example, a common thing is to do a pipeline | while read proc do and then to test for some condition in a case statement. Twice now, I have written scripts where a variable set in the case loop (or anywhere in the while loop, for that matter) won't have been changed from its initial value after the while loop terminates. Can anyone explain to me why this is so. For example, I have a little script that will log me off of my 630 terminal by nohup-ing the login process - as long as only insignificant processes are still running: # # This script will shutdown my account # PATH=$HOME/bin:/bin:/usr/bin:/usr/lbin options="xd" arglist="" optlist="" set -- `getopt $options $*` if [ $? != 0 ]; then echo "Usage: $0 [$options]" exit -1 fi for arg in $*; do case $arg in --) shift break ;; -d) execute=2 export execute shift ;; -x) execute=1 export execute shift ;; esac done ps -u$LOGNAME | awk '{print $4}' | while read proc do execute=3 case $proc in sysmon | awk | ksh | ps | layers | COMMAND) ;; "") echo '\007\007' defunct process? execute=0 export execute ;; *) echo '\007\007' $proc still running execute=0 export execute ;; esac done echo execute is $execute if [ $? -eq 0 ]; then if [ ${execute:-0} -eq 1 ]; then kill -1 ${fatherpid:-"fatherpid not set"} else echo ${fatherpid:-"fatherpid not set"} fi fi But, at the echo, it can be seen that execute never got set during the while loop. It's driving me crazy. Any help would be appreciated.