# bash completion for duser # shellcheck shell=bash _duser_users() { local min max min=$(awk '$1 == "UID_MIN" { print $2 }' /etc/login.defs 2>/dev/null) max=$(awk '$1 == "UID_MAX" { print $2 }' /etc/login.defs 2>/dev/null) getent passwd | awk -F: -v min="${min:-1000}" -v max="${max:-60000}" '$3 >= min && $3 <= max { print $1 }' } _duser() { local cur=${COMP_WORDS[COMP_CWORD]} cmd=${COMP_WORDS[1]:-} if ((COMP_CWORD == 1)); then mapfile -t COMPREPLY < <(compgen -W "list add del passwd lock unlock key groups help" -- "$cur") return fi case $cmd in add) if [[ ${COMP_WORDS[COMP_CWORD - 1]} == --key ]]; then mapfile -t COMPREPLY < <(compgen -f -- "$cur") else mapfile -t COMPREPLY < <(compgen -W "--admin --key --no-password --password-stdin" -- "$cur") fi ;; del | passwd | lock | unlock | key | groups) if ((COMP_CWORD == 2)); then mapfile -t COMPREPLY < <(compgen -W "$(_duser_users)" -- "$cur") elif [[ $cmd == del ]]; then mapfile -t COMPREPLY < <(compgen -W "--home" -- "$cur") elif [[ $cmd == key ]] && ((COMP_CWORD == 3)); then mapfile -t COMPREPLY < <(compgen -W "list add rm" -- "$cur") elif [[ $cmd == key && ${COMP_WORDS[3]} == add ]]; then mapfile -t COMPREPLY < <(compgen -f -- "$cur") elif [[ $cmd == groups ]]; then # +GROUP and -GROUP local sign=${cur:0:1} [[ $sign == [+-] ]] || sign=+ mapfile -t COMPREPLY < <(compgen -P "$sign" -W "$(getent group | cut -d: -f1)" -- "${cur#[+-]}") fi ;; esac } complete -F _duser duser