unable to access a variable not local to a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unable to access a variable not local to a while loop
# 1  
Old 12-23-2008
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  
Old 12-24-2008
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  
Old 12-24-2008
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  
Old 12-24-2008
You can avoid the use of a pipe and cat is redundant in such loops:

Code:
while read fileline
do
  ...
done < ${filename}

Regards
# 5  
Old 12-24-2008
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

Smilie
# 6  
Old 12-24-2008
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
# 7  
Old 12-25-2008
Thanks Chris
This is working perfect for me

Thanks for you too Franklin You guyz have helped a lot for a beginner like me
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Solaris local access restriction other than sshd_config?

Hi All, As part of LDAP implementation we need to restrict users/groups locally on solaris machine: Options tried: sshd_config: as far as my testing it is restricting either user or group, as per the first preference. pam_access.so by default I am unable to find(need some help if this is... (0 Replies)
Discussion started by: Sridaran
0 Replies

2. Shell Programming and Scripting

Unable to compare to a previous value of a variable in a while loop for a file

Hello All, I'm working on a script that will actually read a file consisting of data like the below:(ReportID,Sub_reportID,Sub_reportName) 1,1,ABC 1,2,DEF 1,3,GHI 2,1,JKL 2,2,MNO 3,1,PQR I want to read the Sub Report details for a Report_ID using while loop and write these values into... (6 Replies)
Discussion started by: venkat_reddy
6 Replies

3. Shell Programming and Scripting

Unable to set Global variable outside while loop

Below is my code: count=0 if ... ... else ... find * -prune -type d | sort -r -n | while read d; do count=1 if ; then echo "Count1:$count" ... ... break 2; fi ... done ... fi echo "Count2:$count" (9 Replies)
Discussion started by: mohtashims
9 Replies

4. Red Hat

NFS share full access for local user

Hi All, Is it possible to give full access for a normal user in a NFS share? If its not possible is there a trick with which I can make it work? Thanks in advance Shyam (1 Reply)
Discussion started by: shyam2j
1 Replies

5. Homework & Coursework Questions

Remote Access vs Local Access

Dear Friends, This is not a problem , it's a course work (UNIX scenario).... As part of it I am searching Remote Attacks and find points as 1. Exploiting a listening service 2. Routing through Unix system that is providing security between 2 or more networks 3. User initiated Remote execution... (1 Reply)
Discussion started by: anespa
1 Replies

6. Ubuntu

Local Networking no longer possible-Unable to mount location

Unable to mount location Can anyone help with this please? Since recently installing 12.04 from a downloaded live CD onto my desktop computer, I have been unable to re-establish a network connection with my other... (2 Replies)
Discussion started by: Royalist
2 Replies

7. Red Hat

Understanding local access to NFS export

Hello, I've inherited an NFS setup that allows external servers to write to an NFS share on a Centos box. Here is an example line from /etc/exports (there are four entries that only are different based on server IP adress). /exports/foobar... (4 Replies)
Discussion started by: KickstartUF
4 Replies

8. Red Hat

Restrict local users to access ftp

Hi, I had installed vsftp in rhel5 and i want to restrict all the local users from accessing the ftp. i want to allow specific users to access the ftp server. Request you to please help. Thanks & regards Arun (1 Reply)
Discussion started by: Arun.Kakarla
1 Replies

9. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

10. Shell Programming and Scripting

Unable to access variable outside loop

I am unable to access the value set inside the loop from outside loop . Thought of taking this to forum , I had seen other replies also , where a pipe takes the execution to another shell and mentioned thats the reason we do not get the variable outside loop . But I am getting an issue and I am... (1 Reply)
Discussion started by: Armaan_S
1 Replies
Login or Register to Ask a Question