3 # askpass.sh by Felix Kästner, GPL3
6 # in $* there could be the prompt-message (from an invoking program like sudo)
7 # delete any trailing space or colon and write it to stderr
8 promptmsg="enter password:"
9 [ "$*" != "" ] && promptmsg="${*% }"
10 echo -en "$0: ${promptmsg%:}: " >&2
14 cx='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # chars that can be echoed as feedback for a keypress, there have been problems when using special chars
18 # set IFS to newline and read a character
19 # -s silent, -r \ is taken literal, not as escape character, -n 1 only one char is read
20 IFS=$'\n' read -s -r -n 1 char
23 # backspace was pressed
24 # deletes the last character from $pw and moves the cursor one char back, deleting this char, but don't delete more chars than available
26 [ $stars -gt 0 ] && { ((stars--)); echo -en "\b \b" >&2; }
29 # ctrl + d was pressed, this means abort/cancel
30 # delete all echoed characters, unset variables and exit
31 while [ $((stars--)) -gt 0 ]
36 unset pw char stars rnd cx
41 # add the char to pw and echo some chars as visual feedback
43 ((rnd = RANDOM % 3 + 1)) # bash specific
45 # echos $rnd much of the characters from $cx
46 while [ $((rnd--)) -gt 0 ]
48 echo -n "${cx:$(($RANDOM % ${#cx})):1}" >&2
52 # no new character, so pw is completed
53 # delete the echoed characters
54 remain=$(($RANDOM % 4 + 6))
55 while [ $((stars--)) -gt $remain ]
64 # write a newline to stderr and the password to stdout
67 unset pw char stars rnd cx remain