aboutsummaryrefslogtreecommitdiff
path: root/bin/cf
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-09-03 13:30:39 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-09-03 13:30:39 +1200
commitd76e947287ce7e09b006c7ae5d032f163e7bf440 (patch)
treeaafa754e70dee701b868487d9d21b85f8faae567 /bin/cf
parentRemove blink-matching-paren from ~/.inputrc (diff)
downloaddotfiles-d76e947287ce7e09b006c7ae5d032f163e7bf440.tar.gz
dotfiles-d76e947287ce7e09b006c7ae5d032f163e7bf440.zip
Add -o option to cf(1df)
Diffstat (limited to 'bin/cf')
-rwxr-xr-xbin/cf32
1 files changed, 29 insertions, 3 deletions
diff --git a/bin/cf b/bin/cf
index 3ff60156..d245fec1 100755
--- a/bin/cf
+++ b/bin/cf
@@ -1,10 +1,32 @@
#!/bin/sh
# Count entries in a given set of directories
+self=cf
+
+# Parse options out
+while getopts 'o' opt ; do
+ case $opt in
+
+ # Print only the count, not the filename
+ o) only=1 ;;
+
+ # Unknown option
+ \?)
+ printf >&2 '%s: Unknown option %s\n' \
+ "$self" "$opt"
+ exit 2
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# Iterate over remaining non-option arguments (directories); default to current
+# directory if none given
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"
+ printf >&2 '%s: %s: not a directory\n' \
+ "$self" "$dir"
ex=1
continue
fi
@@ -21,8 +43,12 @@ for dir in "${@:-.}" ; do
[ "$1" = "$dir"/. ] && shift
[ "$1" = "$dir"/.. ] && shift
- # Print number of parameters
- printf '%u\t%s\n' "$#" "$dir"
+ # Print either just the count, or the count and the dirname
+ if [ -n "$only" ] ; then
+ printf '%u\n' "$#"
+ else
+ printf '%u\t%s\n' "$#" "$dir"
+ fi
done
# Exit non-zero if a non-directory was given as an argument