I want to write a script that would count number of executable files in every folder in variable PATH. My code:
#!/bin/bash
IFS=":"
for directory in $PATH; do
files=0
ls -l $directory | while read rights x name group siz m d h name; do
if [ `echo $rights | cut -c1` = "-" ]; then
files=$((${files}+1))
fi
done
echo "Directory ${directory} contains ${files} executable files"
done
I want the echo to process after the end of the while loop and before the start of next for loop, but it always print out number of files = 0. The counting inside the if condition works.