LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-26-2010, 09:14 AM   #1
aitor
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Question [BASH] non-empty variable before loop end, is empty after exiting loop


Hi fellows:

I'm having a quite disturbing trouble with this simple script:

Code:
#!/bin/bash

DIR_HTML_UTF=/home/GGG_index/htmlUtf
vid=""

find $DIR_HTML_UTF -type f -name "*html" | sed 's/[^0-9]//g' | sort -u | while read vid; do
	echo $vid
	vids="$vids,$vid"
	echo "  >> ${vids:1}"
done

echo "=== $vids ==="
And the result of the script is:

Code:
101993
  >> 101993
150115
  >> 101993,150115
150188
  >> 101993,150115,150188
32416
  >> 101993,150115,150188,32416
93939
  >> 101993,150115,150188,32416,93939
===  ===
I can't understand why $vids is empty after de loop. It's just too weird, isn't it?

I'm using a Ubuntu 10.04 64bits box.

Thx!
 
Old 08-26-2010, 09:30 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
It's a simple and common mistake. You used a pipe to feed the output of find into the while loop, which means it runs in a subshell. The subshell can't affect the parent process, so the settings are lost when it exits.

You need to reposition the input as a redirect instead. Something like this (untested):
Code:
while read vid; do
	echo $vid
	vids="$vids,$vid"
	echo "  >> ${vids:1}"
done <<<"$(find $DIR_HTML_UTF -type f -name "*html" | sed 's/[^0-9]//g' | sort -u )"
It's #8 on this list, which includes a link to a more detailed explanation.

http://mywiki.wooledge.org/BashPitfalls
 
1 members found this post helpful.
Old 08-26-2010, 09:57 AM   #3
aitor
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks a lot, now i see. I was expecting something like that, so i tried to "export" the variable in some way.

I searched for the answer at LQ, but was sort of difficult to figure out how to search such a problem (loop + variable + empty ?). Sorry for reiteration.
 
  


Reply

Tags
bash, empty, loop, pipe, subshell, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
Multiple variable for loop (bash) Phier Programming 9 11-24-2009 08:54 AM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 11:13 AM
Using a variable to control a BASH for loop JohnE1 Programming 15 07-04-2009 03:37 AM
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration