diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-03 13:30:39 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-03 13:30:39 +1200 |
commit | d76e947287ce7e09b006c7ae5d032f163e7bf440 (patch) | |
tree | aafa754e70dee701b868487d9d21b85f8faae567 /bin/cf | |
parent | Remove blink-matching-paren from ~/.inputrc (diff) | |
download | dotfiles-d76e947287ce7e09b006c7ae5d032f163e7bf440.tar.gz dotfiles-d76e947287ce7e09b006c7ae5d032f163e7bf440.zip |
Add -o option to cf(1df)
Diffstat (limited to 'bin/cf')
-rwxr-xr-x | bin/cf | 32 |
1 files changed, 29 insertions, 3 deletions
@@ -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 |