~dricottone/my-utils

ref: 581955c8fa8a187f983dc223c947f12e9f9ad474 my-utils/core/ctdir -rwxr-xr-x 966 bytes
581955c8Dominic Ricottone Fix mktar 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/sh

name="ctdir"
version="1.0"
usage_message="Usage: ctdir TARGET [OPTIONS]"
help_message=$(/usr/bin/cat <<-EOF
	Count entries in target directory(ies)
	Usage: ctdir TARGETS [..] [OPTIONS]
	Options:
	 -h, --help     print this message and exit
	 -q, --quiet    suppress error messages
	 -v, --version  print version number and exit
EOF
)

. /usr/local/lib/myminiparse.sh

# error if no directory names given
if [ "$#" -eq 0 ]; then
  (>&2 /usr/bin/printf "%s\n" "$usage_message")
  exit 1
elif [ "$#" -eq 1 ] && [ "$quiet" -eq 1 ]; then
  exit 1
fi

# loop through arguments
code=0
for arg; do
  case "$arg" in
  -q|--quiet)
    #ignore these
    ;;

  *)
    # main routine
    if [ ! -d "$arg" ]; then
      if [ "$quiet" -eq 0 ]; then
        (>&2 /usr/bin/printf "%s: No such directory '%s'\n" "$name" "$arg")
      fi
      code=1
    else
      /usr/bin/ls -1A "$arg" 2>/dev/null | wc -l
    fi
    ;;
  esac
done

# exit with stored code
exit "$code"