Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-08-2017, 02:33 AM   #1
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.

#!/usr/local/bin/bash -x

#takes One $macro after other from macro_names.txt file
for macro in $(cat macro_names.txt); 
	#Takes $files from the current path in a recursive manner 
    find . -type f -name "*.c" -o -name "*.h" | while read files
		#Counts the number of times $macro is present in $files
         CurrFileTotalCount=$(cat "${files}" | grep $macro | wc -l);
		 #Adds the Current file count to $TotalCount
		 #print the $CurrFileTotalCount and $TotalCount PROPERLY
		 echo $macro appears_1 CurrFileTotalCount $CurrFileTotalCount TotalCount $TotalCount
	#Expected to print how many times that $macro is present in the files -- BUT PRINTS $TotalCount AS 0
	echo $macro appears_2 TotalCount $TotalCount
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
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,109

Rep: Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368
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:
while read files

done <$(find . ...)
from the other hand:
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
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,277
Blog Entries: 24

Rep: Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225Reputation: 4225
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...

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

(pan64 types faster than I do...)


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 > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration