#! /usr/bin/env sh main() { # Handle opts opts "$@" shift "$OPTS" unset OPTS # Show help [ "$OPT_HELP_ONLY" ] && usage && exit 0 # Check for dependencies if ! which htpasswd > /dev/null 2>&1 then printf "Missing dependency from \$PATH: %s\n" "htpasswd" exit 1 fi # Check where input comes from if [ -n "$1" ] then input=$1 elif [ ! -t 0 ] then input=$(cat) else usage exit 2 fi # Generate output htpasswd -bnBC "${OPT_COST:-10}" "" "$input" | tr -d ':\n' printf "\n" } opts() { OPTS=0 while getopts ":c:h" opt do case "$opt" in h) OPT_HELP_ONLY=1 ;; c) OPT_COST=$OPTARG ;; *) printf "Invalid option passed: %s\n" "$OPTARG" >&2 ;; esac done unset opt } usage() { cat <] Generate a bcrypt hash of the input. Options: -c Set the cost. Defaults to 10. -h Show this help text and exit. EOF } main "$@"