#!/bin/sh # Count entries in a given set of directories for dir in "${@:-.}" ; do # Warn if a non-directory was given, flag errors, but continue if ! [ -d "$dir" ] ; then printf >&2 'cf: %s: not a directory\n' "$dir" ex=1 continue fi # Add files matching glob, shift them off if unexpanded (first and only # entry doesn't exist) set -- "$dir"/* [ -e "$1" ] || shift # Add dot files, shift off the "." and ".." entries (sh(1) implementations # seem to vary on whether they include these) set -- "$dir"/.* "$@" [ -e "$1" ] || shift [ "$1" = "$dir"/. ] && shift [ "$1" = "$dir"/.. ] && shift # Print number of parameters printf '%u\t%s\n' "$#" "$dir" done # Exit non-zero if a non-directory was given as an argument exit "${ex:-0}"