#!/usr/bin/bash function usage() { local just_help=$1 local missing_required=$2 local invalid_argument=$3 local invalid_option=$4 local help="Usage: genpass -c COUNT [OPTION] [SET] Generate random password from /dev/urandom using the character set chosen Option (pick one character set):" local help_options="\t\-a ,\--all\ all printable characters, not including space \t\-lt ,\--limited\ all except special characters [A-Za-z0-9%+,-./:=@^_] \t\-b32 ,\--base32\ all valid base32 characters [A-Z2-7=] \t\-b64 ,\--base64\ all valid base64 characters [A-Za-z0-9+/] \t\-an ,\--alphanumeric\ letters and digits \t\-al ,\--alpha\ letters \t\-u ,\--upper\ upper case letters \t\-l ,\--lower\ lower case letters \t\-n ,\--numeric\ digits \t\-h ,\--hex\ hexadecimal digits \t\-c ,\--count \ Length of password to generate \t\[SET]\ \ Other character SET compatible with tr " if [ "$missing_required" != "" ] then echo "Missing required argument: $missing_required" fi if [ "$invalid_option" != "" ] && [ "$invalid_value" = "" ] then echo "Invalid option: $invalid_option" elif [ "$invalid_value" != "" ] then echo "Invalid value: $invalid_value for option: --$invalid_option" fi echo -e " " echo "$help" echo "$help_options" | column -t -s'\' return } function init_args() { REQ_ARGS=("count" ) # get command line arguments POSITIONAL=() while [[ $# -gt 0 ]] do key="$1" case $key in -a|--all) characters="[:graph:]" shift ;; -lt|--limited) characters="A-Za-z0-9%+,-./:=@^_" shift ;; -b32|--base32) characters="A-Z2-7=" shift ;; -b64|--base64) characters="A-Za-z0-9+/" shift ;; -an|--alphanumeric) characters="[:alnum:]" shift ;; -al|--alpha) characters="[:alpha:]" shift ;; -u|--upper) characters="[:upper:]" shift ;; -l|--lower) characters="[:lower:]" shift ;; -n|--numeric) characters="[:digit:]" shift ;; -h|--hex) characters="0-9a-f" shift ;; -c|--count) count="$2" shift 2 ;; *) POSITIONAL+=("$1") # saves unknown option in array shift ;; esac done for i in "${REQ_ARGS[@]}"; do # $i is the string of the variable name # ${!i} is a parameter expression to get the value # of the variable whose name is i. req_var=${!i} if [ "$req_var" = "" ] then usage "" "--$i" exit fi done } init_args $@ if [ -z $characters ]; then if [ -z $POSITIONAL ]; then echo "Missing at least one character set" exit 2 else characters=$POSITIONAL fi fi < /dev/urandom tr -dc $characters | head -c $count