LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2006, 07:14 PM   #1
cool244
LQ Newbie
 
Registered: Mar 2005
Posts: 7

Rep: Reputation: 0
shell variables becoming zero outside the loop


Folks,

I have a simple script, the calcualtions done inside a while loop or lost outside the loop. Here is the script.



Code:
#! /bin/ksh
vmstat 1 6 | sed '1,4d' | while read a b c d e f g h i j k l m n o p junk
do
  echo "CNT is $cnt"
  if [[ $cnt -le 3 ]]; then
    echo "in loop"
    let sum1=$sum1+$a
    let sum2=$sum2+$b
    let sum3=$sum3+$c
    let sum4=$sum4+$d
    echo "###inside if loop values are sum1=${sum1} sum2=${sum2} sume3=${sum3} sum4=${sum4}###"
  fi
    let cnt=$cnt+1
    echo "###outside if loop values are sum1=${sum1} sum2=${sum2} sume3=${sum3} sum4=${sum4}###"
done
    echo "###outside while loop values are sum1=${sum1} sum2=${sum2} sume3=${sum3} sum4=${sum4}###"

The output:/tmp/test1
Code:
CNT is
in loop
###inside if loop values are sum1=0 sum2=0 sume3=0 sum4=10129064###
###outside if loop values are sum1=0 sum2=0 sume3=0 sum4=10129064###
CNT is 1
in loop
###inside if loop values are sum1=0 sum2=0 sume3=0 sum4=20258128###
###outside if loop values are sum1=0 sum2=0 sume3=0 sum4=20258128###
CNT is 2
in loop
###inside if loop values are sum1=0 sum2=0 sume3=0 sum4=30387160###
###outside if loop values are sum1=0 sum2=0 sume3=0 sum4=30387160###
CNT is 3
in loop
###inside if loop values are sum1=0 sum2=1 sume3=0 sum4=40516192###
###outside if loop values are sum1=0 sum2=1 sume3=0 sum4=40516192###
###outside while loop values are sum1= sum2= sume3= sum4=###

As you can sum4 is NULL outside the loop. The same script works in AIX. Can someone please let me know, what is wrong here? I tried both ksh and bash and results are same.

Thank you

Last edited by XavierP; 05-20-2006 at 07:14 AM.
 
Old 05-18-2006, 09:48 PM   #2
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
I'm pretty sure this belongs in PROGRAMMING...
 
Old 05-20-2006, 07:14 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 05-20-2006, 01:09 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Its important to realize that piping '|' causes the shell to startup another instance of the shell (ksh in your case) and pipes the data to its stdin where "read a..junk" picks it up.
When this "subshell" exits, all of its environment is destroyed. So, variables you set outside are readable only inside the subshell, and when the shell exits any modifications you made to the read-only variables are lost. This behavior is normal.

One way to get around this is to write the output generated by the subshell to a temporary file and after the code block read the temp file back in. Another way is to put the whole code block inside a output=`<codeblock>` and echo the results.

I'm interested in hearing what other programmers do, because this is frequently a frustration for myself as well.
 
Old 05-20-2006, 03:33 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Quote:
Originally Posted by randyding
Its important to realize that piping '|' causes the shell to startup another instance of the shell (ksh in your case) and pipes the data to its stdin where "read a..junk" picks it up.
So you'll either have to return the values from the subshell as randyding proposes (although there are probably more beautiful solutions than using a temporary file), or you simply do all the output from the subshell as in the following example (note the parentheses):
Code:
#! /bin/ksh

vmstat 1 6 | sed '1,4d' | 
(
    while read a b c d e f g h i j k l m n o p junk
      do
      echo "CNT is $cnt"
      if [[ $cnt -le 3 ]]; then
	  echo "in loop"
	  let sum1=$sum1+$a
	  let sum2=$sum2+$b
	  let sum3=$sum3+$c
	  let sum4=$sum4+$d
	  echo "###inside if loop values are sum1=${sum1} sum2=${sum2} sume3=${sum3} sum4=${sum4}###"
      fi
      let cnt=$cnt+1
      echo "###outside if loop values are sum1=${sum1} sum2=${sum2} sume3=${sum3} sum4=${sum4}###"
    done
    echo "###outside while loop values are sum1=${sum1} sum2=${sum2} sume3=${sum3} sum4=${sum4}###"
)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell variable in Linux is reset after while loop CBenner Programming 7 05-19-2006 09:46 AM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM
Bash - Getting a for loop to process variables with a space in them davee Linux - General 4 10-08-2005 11:21 AM
BASH variables getting unset outside of loop trevelluk Programming 2 03-25-2005 07:14 AM
[c shell] simple for loop saiz66 Programming 1 09-28-2004 07:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration