Fix subshell variable scope issue in file counting scripts

The count variable was not being updated because the while loops were
running in subshells created by the pipe operator. Variable assignments
in subshells don't affect the parent shell, causing count to remain 0.

Changes:
- list_files.sh: Changed from pipe to here-document with command
  substitution to keep the while loop in the main shell
- list_files_bash.sh: Changed from pipe to process substitution using
  < <(...) syntax to avoid subshell creation for the while loop

Both scripts now correctly display the total number of files found.
This commit is contained in:
Edson Brandi 2025-11-07 21:53:51 +00:00
parent ba961a1fee
commit 712daf6c49

View file

@ -2639,12 +2639,13 @@ set -eu
cd "${HOME}" cd "${HOME}"
count=0 count=0
find . -maxdepth 1 -type f ! -name ".*" -print |
while IFS= read -r f; do while IFS= read -r f; do
fname=${f#./} fname=${f#./}
echo "File found: '$fname'" echo "File found: '$fname'"
count=$((count + 1)) count=$((count + 1))
done done << EOF
$(find . -maxdepth 1 -type f ! -name ".*" -print)
EOF
echo "Total files found: $count" echo "Total files found: $count"
``` ```
@ -2672,12 +2673,11 @@ set -eu
cd "${HOME}" cd "${HOME}"
count=0 count=0
find . -maxdepth 1 -type f ! -name ".*" -print0 |
while IFS= read -r -d '' f; do while IFS= read -r -d '' f; do
fname=${f#./} fname=${f#./}
echo "File found: '$fname'" echo "File found: '$fname'"
count=$((count + 1)) count=$((count + 1))
done done < <(find . -maxdepth 1 -type f ! -name ".*" -print0)
echo "Total files found: $count" echo "Total files found: $count"
``` ```