aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-07-31 19:12:21 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-07-31 20:23:51 +1200
commitf1fc74cdd6fbc4ea632be035cca6239370a6ea55 (patch)
tree77c6c96f868d6a884b3bfc8557f0967c3f4a387b
parentSplit out sls(1) into shell and awk (diff)
downloaddotfiles-f1fc74cdd6fbc4ea632be035cca6239370a6ea55.tar.gz
dotfiles-f1fc74cdd6fbc4ea632be035cca6239370a6ea55.zip
Change cf(1) to POSIX sh and multi args
-rwxr-xr-xbin/cf47
-rw-r--r--man/man1/cf.17
2 files changed, 25 insertions, 29 deletions
diff --git a/bin/cf b/bin/cf
index d1515a03..6b7bd9d8 100755
--- a/bin/cf
+++ b/bin/cf
@@ -1,27 +1,20 @@
-#!/usr/bin/env bash
-
-# Count files
-self=cf
-
-# Specify directory to check, defaults to current dir
-dirname=${1:-"$PWD"}
-
-# Error conditions
-if [[ ! -e $dirname ]] ; then
- printf '%s: %s does not exist\n' \
- "$self" "$dirname" >&2
- exit 1
-elif [[ ! -d $dirname ]] ; then
- printf '%s: %s is not a directory\n' \
- "$self" "$dirname" >&2
- exit 1
-elif [[ ! -r $dirname ]] ; then
- printf '%s: %s is not readable\n' \
- "$self" "$dirname" >&2
- exit 1
-fi
-
-# Count files and print; use dotglob and nullglob so we get an accurate count
-shopt -s dotglob nullglob
-declare -a files=("$dirname"/*)
-printf '%u\t%s\n' "${#files[@]}" "$dirname"
+#!/bin/sh
+# Count entries in a given set of directories
+ex=0
+for dir in "${@:-.}" ; do
+ if ! [ -d "$dir" ] ; then
+ printf >&2 'cf: %s: not a directory\n' "$dir"
+ ex=1
+ continue
+ fi
+ c=0
+ for ent in "$dir"/* "$dir"/.* ; do
+ [ -e "$ent" ] || continue
+ case $ent in
+ */.|*/..) ;;
+ *) c=$((c+1)) ;;
+ esac
+ done
+ printf '%u\t%s\n' "$c" "$dir"
+done
+exit "$ex"
diff --git a/man/man1/cf.1 b/man/man1/cf.1
index 4c30c9d8..0bedb19f 100644
--- a/man/man1/cf.1
+++ b/man/man1/cf.1
@@ -6,9 +6,12 @@
.B cf
.br
.B cf /path/to/dir
+.br
+.B cf dir1 dir2
.SH DESCRIPTION
.B cf
-counts all the entries in a given directory using Bash glob expansion and
-prints the count. It defaults to the current directory.
+counts all the entries in the given directories using glob
+expansion and prints the count. It defaults to the current
+directory.
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>