LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2008, 01:39 PM   #1
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
working with variables- how?


hi i am working in bash.

few lines of code is :
1.num=1

2.grep '^ID' vdat.txt | while read line
3.do
4.field_3=$(echo "$line" | awk '{print $3}')
5.echo $field_3
6.array[$num]=$field_3
7.echo ${array[$num]}
8.num=`expr $num + 1`
9.done
10.echo $num

the value of num at line 10 is 1 even after manipulating it in the line 8 inside for loop.. lets say it is a for loop with i=1, i<=4 i++ and i manipulate the value of num inside the forloop.

how can get the value of num as 4 outside the loop?

im having problem as the num inside the loop becomes local and doesnt effact the global variable

am a newbie.. please help me.
 
Old 06-09-2008, 02:10 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by pdklinux79 View Post
hi i am working in bash.

few lines of code is :
1.num=1

2.grep '^ID' vdat.txt | while read line
3.do
4.field_3=$(echo "$line" | awk '{print $3}')
5.echo $field_3
6.array[$num]=$field_3
7.echo ${array[$num]}
8.num=`expr $num + 1`
9.done
10.echo $num

the value of num at line 10 is 1 even after manipulating it in the line 8 inside for loop.. lets say it is a for loop with i=1, i<=4 i++ and i manipulate the value of num inside the forloop.

how can get the value of num as 4 outside the loop?

im having problem as the num inside the loop becomes local and doesnt effact the global variable

am a newbie.. please help me.
OK, the problem is that when you use a pipe, bash forks out a subshell which performs your while loop. Variables, including your $num are specific to that subshell and when the loop is finished, you see the original $num that has never been modified.

On a side note, this line num=`expr $num + 1` seems strange. Do you mean something like num=$((num+1)) ?
 
Old 06-09-2008, 02:27 PM   #3
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
yes.. it is num= num+1..

so how do i get the effect i want?is there any way?
 
Old 06-09-2008, 02:35 PM   #4
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by pdklinux79 View Post
yes.. it is num= num+1..

so how do i get the effect i want?is there any way?
Well, grep the file without a pipe. Something like this

Code:
1.num=1

2.while read line
3.do
4.field_3=$(echo "$line" | awk '{print $3}')
5.echo $field_3
6.array[$num]=$field_3
7.echo ${array[$num]}
8.num=$((num+1))
9.done < <(grep '^ID' vdat.txt)
10.echo $num
should do it.
 
Old 06-09-2008, 02:51 PM   #5
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Thank you Uncle Theodre so much.. it is working..
 
Old 06-10-2008, 04:50 AM   #6
CanOfPoke
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Rep: Reputation: 0
I had same problem it all works fine now though.
 
  


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
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
Environment variables not working iansoundz Linux - General 3 12-02-2005 02:28 PM
Variables invloved with working camera/usb storage device connection jimdaworm Slackware 2 04-25-2004 11:12 AM
exporting variables under bash isn't working! dtheorem Programming 6 03-11-2004 08:35 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:53 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