LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-30-2008, 08:26 AM   #1
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Rep: Reputation: 15
Bash: what happens to my variables?


Here's my code:

Code:
counter=0
head -n500 esn.txt | while read line
do
	let "counter += 1"
	if [ "$counter" -eq 1 ]
	then
		email=${line:55}
		echo $email
	elif [ "$counter" -eq 4 ]
	then
		ESN=$line
		echo $ESN
	fi
done
echo $ESN
echo $email
why at the end when I echo $ESN and $email are they blank? I'm missing something...

Thanks,
Eric
 
Old 01-30-2008, 08:42 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
as you create them inside of the loop, they are destroyed due to scoping once you leave it. if you put "email=whatever" at the start and then *change* it in the loop then it'll persist.
 
Old 01-30-2008, 08:50 AM   #3
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Try changing your loop to
Code:
counter=0
while read line
do
	...
done < <(head -n500 esn.txt)
echo $ESN
echo $email
 
Old 01-30-2008, 09:04 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
It's all about scope: as acid_kewpie points out, variables created inside a loop are only local to that loop. You must create them first, then use them within (and possibly outside) the loop.
 
Old 01-30-2008, 09:11 AM   #5
elinenbe
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
rupertwh: Thanks... I did that suggestion and everything worked as it should. I also now understand where I went wrong. Thanks everyone.
 
Old 01-30-2008, 02:32 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually it's not the loop which retains local variables: it is the pipe redirection which opens a subshell that uses local variables
Code:
head -n500 esn.txt | while ...
do
      #
      # this is the subshell scope due to pipe redirection
      #
done
When using "process substitution" (as posted by rupertwh) a new subshell is not called and the modified variables are retained outside the block of code. Another way to do the same is to link a file descriptor to the input file (or part of it if using the command head with process substitution). The option -u of read let you take input from the specified descriptor:
Code:
#!/bin/bash
counter=0
exec 3< esn.txt
while read -u 3 line
do
	let "counter += 1"
	if [ "$counter" -eq 1 ]
	then
		email=${line:55}
		echo $email
	elif [ "$counter" -eq 4 ]
	then
		ESN=$line
		echo $ESN
	fi
done
echo $ESN
echo $email
exec 3<&-
 
  


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
Help With Variables In Bash jamie_h Programming 3 11-01-2006 08:18 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
variables in bash haddad Linux - General 6 09-22-2004 05:29 PM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM

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

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