LinuxQuestions.org
Review your favorite Linux distribution.
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 05-08-2017, 02:33 AM   #1
csakthikumar
LQ Newbie
 
Registered: May 2017
Posts: 10

Rep: Reputation: Disabled
Bash script counting doesn't work


Hi All,
I wrote the below bash code which will count the number of times a specific keyword(macro) is found in the list of files(in recursive fashion). But what happens is when I execute the script in "echo appears_1" the $CurrFileTotalCount and $TotalCount prints properly BUT in "echo appears_2" the value of $TotalCount is displayed as 0.

Code:
#!/usr/local/bin/bash -x
TotalCount=0

#takes One $macro after other from macro_names.txt file
for macro in $(cat macro_names.txt); 
do
	#Takes $files from the current path in a recursive manner 
    find . -type f -name "*.c" -o -name "*.h" | while read files
    do
		#Counts the number of times $macro is present in $files
         CurrFileTotalCount=$(cat "${files}" | grep $macro | wc -l);
		 #Adds the Current file count to $TotalCount
		 TotalCount=$(($TotalCount+$CurrFileTotalCount));
		 #print the $CurrFileTotalCount and $TotalCount PROPERLY
		 echo $macro appears_1 CurrFileTotalCount $CurrFileTotalCount TotalCount $TotalCount
    done
	#Expected to print how many times that $macro is present in the files -- BUT PRINTS $TotalCount AS 0
	echo $macro appears_2 TotalCount $TotalCount
	TotalCount=0;
done
Above is the code I think the scope is the problem I tried using "declare -i" but it is also not working. Can you please point out what is wrong with the script.
 
Old 05-08-2017, 03:14 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,820

Rep: Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671Reputation: 5671
the trick is: the while loop runs in another shell, because the result of find is piped into it. This subshell cannot set TotalCount of its parent.
The solution:
Code:
while read files
do
...

done <$(find . ...)
from the other hand:
Code:
CurrFileTotalCount=$(cat "${files}" | grep $macro | wc -l);
# can be written as:
CurrFileTotalCount=$(grep -c $macro "${files}");
 
1 members found this post helpful.
Old 05-08-2017, 03:18 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,713
Blog Entries: 11

Rep: Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747
Piping the output of find into the while block creates a separate execution shell. It cannot pass variables back to its parent when it is done.

You can use process substitution to avoid spawning the new shell, something like this should work...

Code:
while; do
...
done< <(...)
I would also avoid the unnecessary use of cat and suggest quoting the output statements.

(pan64 types faster than I do...)
 
  


Reply

Tags
bash, script


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
bash script doesn't work. L_Carver Linux - Newbie 11 02-27-2017 02:31 AM
[SOLVED] My bash script doesn't work massy Programming 10 02-03-2014 06:59 AM
bash script to start process doesn't work? shams Linux - General 4 08-18-2013 11:09 PM
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 03:06 AM

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

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