The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Not access variable outside loop when a reading a file ricardo.ludwig Shell Programming and Scripting 4 05-23-2009 11:14 PM
'while' loop does not change local variables?! alex_5161 Shell Programming and Scripting 3 11-20-2008 02:06 PM
While loop - The sum seems to be local eagercyber Shell Programming and Scripting 2 03-26-2008 04:06 AM
Unable to access home area Mr Pink SUN Solaris 1 04-15-2007 05:57 PM
unable to access hpfs/ntfs properly moxxx68 UNIX for Dummies Questions & Answers 1 11-08-2005 04:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-23-2008
codeman007 codeman007 is offline
Registered User
  
 

Join Date: Sep 2008
Location: US
Posts: 32
unable to access a variable not local to a while loop

I have a while loop like this


cat ${filename} | while read fileline

do
...

done

I need to access a variable value $newfile inside this while loop
How will i do that??
  #2 (permalink)  
Old 12-24-2008
geekosaur geekosaur is offline
Registered User
  
 

Join Date: Oct 2008
Location: Pittsburgh, PA, USA
Posts: 20
You should be able to read any variable that has been set at that point. Assigning to it won't work because the pipe forces the while loop to run in a subshell; assignments will be visible within the while loop but will be lost when the loop is exited. I generally use temporary files to work around this, but sometimes run it in a "eval $(expression)" and have it echo the variable assignment(s) to stdout. (The latter avoids the various issues with temporary files but is rather more confusing to most people.)
  #3 (permalink)  
Old 12-24-2008
codeman007 codeman007 is offline
Registered User
  
 

Join Date: Sep 2008
Location: US
Posts: 32
i didnt get this really. I understood since i am using the pipe ; variables declared outside the while loop are not accessible inside. Is there any means to pass the varible inside the while loop...
  #4 (permalink)  
Old 12-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
You can avoid the use of a pipe and cat is redundant in such loops:

Code:
while read fileline
do
  ...
done < ${filename}
Regards
  #5 (permalink)  
Old 12-24-2008
codeman007 codeman007 is offline
Registered User
  
 

Join Date: Sep 2008
Location: US
Posts: 32
Franklin I tried this before as i saw it in some other post of yours but my requirement is different


cat ${filename} | while read fileline

do
size=`echo $fileline | cut -d "," -f2`
value=`echo $fileline | cut -d "," -f3`
info=`echo $fileline | cut -d "," -f4`

variable=$passedvariable

done
I need to read each line from the ${filename} and at the same time get the value of $passedvariable which is declared outside while loop inside the while

  #6 (permalink)  
Old 12-24-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

Code:
variable=1 && while IFS="${IFS}," read size value info 
do 
  echo $size $value $info
  echo "$variable"
done < ${filename}
$variable is declared outside and can be accessed from inside
the loop.

HTH Chris
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0