Created
March 16, 2015 22:13
-
-
Save pkutzner/90cda2ff17c688cc1a52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getpassword() { | |
local charcount=0 | |
local password='' | |
local CHAR='' | |
local PROMPT='' | |
stty -echo | |
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR | |
do | |
# Enter - accept password | |
if [[ $CHAR == $'\0' ]]; then | |
break | |
fi | |
# Backspace | |
if [[ $CHAR == $'\177' ]]; then | |
if [ $charcount -gt 0 ]; then | |
charcount=$((charcount-1)) | |
PROMPT=$'\b \b' | |
password="${password%?}" | |
else | |
PROMPT='' | |
fi | |
else | |
charcount=$((charcount+1)) | |
PROMPT='*' | |
password+="$CHAR" | |
fi | |
done | |
stty echo | |
echo $password | |
} | |
while IFS= true; do | |
printf "MySQL Password: " | |
MPASSWD="$(getpassword)" | |
echo | |
printf "MySQL Password (again): " | |
TMPASSWD="$(getpassword)" | |
echo | |
[ "$MPASSWD" = "$TMPASSWD" ] && break | |
echo "Please try again." | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment