Skip to content

Instantly share code, notes, and snippets.

@pkutzner
Created March 16, 2015 22:13
Show Gist options
  • Save pkutzner/90cda2ff17c688cc1a52 to your computer and use it in GitHub Desktop.
Save pkutzner/90cda2ff17c688cc1a52 to your computer and use it in GitHub Desktop.
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