Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

jvscode [[at]] fastmail [[dot]] fm

1 total

On This Page:

  1. 1 Bash password prompt

Bash password prompt

#!/bin/bash

# cf. Creating password prompts using Bash, http://ubuntuforums.org/showthread.php?t=269592

# For yet another example of how to use keys in shell scripts see:
# arrows - example how to use cursor keys in ksh scripts,
# http://www.shelldorado.com/scripts/quickies/arrows.txt


stty_orig=$(stty -g) 

# Init some stuff...
pass=''
blank='false'

# Main loop executed once for each char typed...
while [[ "$blank" != "true" ]]; do
   stty -icanon -echo
   c=$(dd bs=6 count=1 2> /dev/null)

   # Check for a CR.
   if [ -z "$(printf -- "$c" | tr -d "\r\n")" ]; then
      blank='true'
   else
      stty echo
      echo -n "*"
      pass="$pass$c"
      stty -echo
   fi
done

stty icanon echo

echo
printf -- "$pass\n"

stty "$stty_orig"

exit 0

1 total

On This Page:

  1. 1 Bash password prompt