FDD-book/examples/part-01/ch03-a-gentle-introduction-to-unix/portability/list_files.sh

22 lines
545 B
Bash
Raw Permalink Normal View History

#!/bin/sh
# list_files.sh - count files in home directory
#
# DELIBERATELY NAIVE: this version is the "naive approach that breaks"
# in the chapter's "Shell Portability" section. It counts a file with
# a newline in its name as two separate files. Compare with the
# _bash, _posix, and _exec variants in this directory.
set -eu
cd "${HOME}"
count=0
while IFS= read -r f; do
fname=${f#./}
echo "File found: '$fname'"
count=$((count + 1))
done << EOF
$(find . -maxdepth 1 -type f ! -name ".*" -print)
EOF
echo "Total files found: $count"