#!/usr/bin/env bash
set -e
set -o pipefail
usage() {
echo 'dir-size [-h]
[du options...]
prints the number of bytes in DIR
additional (after first) arguments are passed to du'
exit "${1:-0}"
}
[[ "$1" == '-h' ]] && usage
DIR="$1"
if [[ -z "$DIR" ]]; then
echo -e 'dir-size: error: Pass directory as first argument\n' >&2
usage 1
fi
shift
if [[ ! -d "$DIR" ]]; then
printf '%s is not a directory\n' "$DIR" >&2
exit 1
fi
du -bcs "$@" "$DIR" | awk 'END{print $1}' || exit $?