Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-14-2008, 09:17 PM   #1
LQ Newbie
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Problem displaying the value of a counter

I am brand new to Linux and Shell Scripting, so this may be a very basic problem, but I am having this one issue with my script (using the BASH shell).

I am trying to display the value of my count, but it always reverts back to the default value of 0

This is my code

cd /home/dave
ls-1 > directory.txt

cat directory.txt |while read line; do
if [ -d ${line} ]; then
let COUNTER+=1

echo The total is: $COUNTER
The point is to count the number of directory folders, and the echo within my if statement displays correctly, but when I get to my final statement it displays "The total is: 0". Even though the previous value displayed was 5.

I am totally confused why it is doing this, any help would be much appreciated.
Old 10-14-2008, 09:31 PM   #2
Senior Member
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,709

Rep: Reputation: 744Reputation: 744Reputation: 744Reputation: 744Reputation: 744Reputation: 744Reputation: 744
The problem is that the COUNTER is in a loop and the loop is in a piped subproces. In other words, while...done is a different subprocess and hence the scope of COUNTER is limited to the loop.

while read line; do
if [ -d ${line} ]; then
let COUNTER+=1
done < directory.txt
Not tested, but it should be somethinh like that with the '< file' at the end of the loop.

Test your script with 'sh -x yourscript' to see the trace of the program.

Old 10-14-2008, 09:55 PM   #3
LQ Newbie
Registered: Oct 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks, that was exactly what I needed. The quick response is greatly appreciated.
Old 10-14-2008, 10:13 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
for line in `cat directory.txt`
instead of the piped while


bash, script, shell

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
Problem with Compiz displaying desktop thekid Linux - Desktop 0 07-22-2008 01:14 PM
Cedega Counter Strike Crash Problem danielscott Linux - Games 1 12-19-2005 07:54 PM
Weird, inconsistent Counter-strike problem Micro420 Linux - Games 3 07-10-2004 02:36 PM
WINE + Counter-strike,LAN game problem vykupitel Linux - Software 1 07-30-2003 10:26 AM
Counter-Strike Install Problem Nefarious Linux - Software 9 04-23-2003 01:52 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration