LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-14-2008, 08:17 PM   #1
dgiles79
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
Code:
#!/bin/bash
COUNTER=0

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

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

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, 08:31 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

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

Suggestion:
Code:
while read line; do
if [ -d ${line} ]; then
let COUNTER+=1
echo $COUNTER
fi
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.

jlinkels
 
Old 10-14-2008, 08:55 PM   #3
dgiles79
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, 09:13 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,232

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Or
Code:
for line in `cat directory.txt`
do
...
instead of the piped while
 
  


Reply

Tags
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 12:14 PM
Cedega Counter Strike Crash Problem danielscott Linux - Games 1 12-19-2005 06:54 PM
Weird, inconsistent Counter-strike problem Micro420 Linux - Games 3 07-10-2004 01:36 PM
WINE + Counter-strike,LAN game problem vykupitel Linux - Software 1 07-30-2003 09:26 AM
Counter-Strike Install Problem Nefarious Linux - Software 9 04-23-2003 12:52 PM


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