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 01-07-2010, 12:25 PM   #1
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
bash: resetting of global variable?


Hi,

I the following code to search a directory recursively:

Code:
declare -i COUNTER=0

# start recursive search
recurse ()
{
echo counter: $COUNTER
	ls "$2" | while read a
	do
		if [ -d "$2"/"$a" ]
		then
			if [[ "$a" == "." || "$a" == ".." ]]
			then
				continue
			else
				recurse "$1" "$2"/"$a"
			fi
		else
			let "COUNTER += 1"
			echo "$2"/"$a" >> "$1" # for debugging only: put them into file to verify if all files are found
		fi
	done
}

recurse $LOGPATH $SEARCHDIR
Now the code works fine but COUNTER is not being incremented. The output I get is:
Code:
counter: 0
counter: 0
counter: 0
counter: 0
It appears that COUNTER is being treated like a local variable. Until now I thought local variables have to be defined explicitly as local. So I am a bit confused now.
 
Old 01-07-2010, 12:54 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Try COUNTER=$(($COUNTER + 1)).

Wouldn't it easier just to do a ls -r $1?
 
Old 01-07-2010, 01:05 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Original Poster
Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by PTrenholme View Post
Try COUNTER=$(($COUNTER + 1)).
Hi,

I already tried COUNTER=$(($COUNTER + 1)). Does not work.

Quote:
Wouldn't it easier just to do a ls -r $1?
Did you mean ls -R $2?

Yes, that would be definetly easier. But I am still new to bash and the main purpose for me at this point is educational value rather than efficiency.
Besides, it is still interesting to find out where the bug is.
 
Old 01-07-2010, 01:18 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
This sort of thing is what "find" is for
http://mywiki.wooledge.org/UsingFind
 
Old 01-07-2010, 01:25 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Original Poster
Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by tuxdev View Post
This sort of thing is what "find" is for
http://mywiki.wooledge.org/UsingFind
As I already said, there might be better ways to do and I could work around the problem. But I still like to know why COUNTER is being reset to zero every time the function is reentered. It is being incremented by 'let' but it seems that this is not persistent.
 
Old 01-07-2010, 01:36 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
http://mywiki.wooledge.org/BashFAQ/024
http://mywiki.wooledge.org/ParsingLs

Doing it the right way is not "working around the problem"
 
1 members found this post helpful.
Old 01-07-2010, 02:26 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Original Poster
Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by tuxdev View Post
http://mywiki.wooledge.org/BashFAQ/024
http://mywiki.wooledge.org/ParsingLs

Doing it the right way is not "working around the problem"
Hi tuxdev,

the links were really helpful. I used a recursive function and not a recursive call to the same script to avoid the subshell problem. But there it was again.

So this how I do it now:

Code:
find $SEARCHDIR ! -type d >> $LOGPATH
COUNTER=`wc -l $LOGPATH | sed -e 's% .*%%g'`
 
Old 01-07-2010, 07:40 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
There's a way to make it not run in a subshell environment but it's not a good idea since it will require to spawn multiple processes at runtime. There could also be a limit in the number of file descriptors that can be used.

Code:
declare -i COUNTER=0

# start recursive search
recurse ()
{
echo counter: $COUNTER
	{
		while read a
		do
			if [ -d "$2"/"$a" ]
			then
				if [[ "$a" == "." || "$a" == ".." ]]
				then
					continue
				else
					recurse "$1" "$2"/"$a"
				fi
			else
				let "COUNTER += 1"
				echo "$2"/"$a" >> "$1" # for debugging only: put them into file to verify if all files are found
			fi
		done
	} < <(exec ls "$2")
}

recurse $LOGPATH $SEARCHDIR
 
  


Reply


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
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 06:08 AM
bash/sh: global or local variable with for (loop) isssue frenchn00b Programming 9 11-06-2008 07:24 AM
Using global variable in awk (bash script) kopeda Programming 2 04-24-2007 01:47 AM
problems with a global variable javier_ccs Programming 2 06-21-2005 12:18 PM
python global variable? goestin Programming 1 01-20-2005 06:52 AM


All times are GMT -5. The time now is 05:49 PM.

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