Merge pull request #12 from dylanaraps/sread

pash: Added password confirm to input
This commit is contained in:
dylan 2019-11-30 18:13:52 +02:00 committed by GitHub
commit 109eb7c5a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 9 deletions

32
pash
View File

@ -19,16 +19,15 @@ pw_add() {
dd ibs=1 obs=1 count="${PASH_LENGTH:-50}" 2>/dev/null) dd ibs=1 obs=1 count="${PASH_LENGTH:-50}" 2>/dev/null)
else else
printf 'Enter password: ' # 'sread()' is a simple wrapper function around 'read'
# to prevent user input from being printed to the terminal.
sread pass "Enter password"
sread pass2 "Enter password (again)"
# Disable terminal printing while the user inputs their # Disable this check as we dynamically populate the two
# password. POSIX 'read' has no '-s' flag which would # passwords using the 'sread()' function.
# effectively do the same thing. # shellcheck disable=2154
stty -echo [ "$pass" = "$pass2" ] || die "Passwords do not match"
read -r pass
stty echo
printf '\n'
fi fi
[ "$pass" ] || die "Failed to generate a password" [ "$pass" ] || die "Failed to generate a password"
@ -120,6 +119,21 @@ yn() {
glob "$answer" '[yY]' glob "$answer" '[yY]'
} }
sread() {
# This is a simple wrapper around POSIX 'read' to print
# a prompt and disable printing of user input.
printf '%s: ' "$2"
# Disable terminal printing while the user inputs their
# password. POSIX 'read' has no '-s' flag which would
# effectively do the same thing.
stty -echo
read -r "$1"
stty echo
printf '\n'
}
glob() { glob() {
# This is a simple wrapper around a case statement to allow # This is a simple wrapper around a case statement to allow
# for simple string comparisons against globs. # for simple string comparisons against globs.