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.