-
Notifications
You must be signed in to change notification settings - Fork 4
/
readpass.sh
64 lines (61 loc) · 1.96 KB
/
readpass.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#---------------------------------------------------
# File: ReadPass Function Library
#---------------------------------------------------
#---------------------------------------------------
# Function: getPrompt
# Description: Read a line of input hile echoing on
# the screen.
# parameters: prompt
# usage: myvar="$(getPrompt 'prompt')"
#---------------------------------------------------
function getPrompt() {
prompt="$1 "
read -p "$prompt" input
echo "$input"
}
#---------------------------------------------------
# End getPrompt
#---------------------------------------------------
#---------------------------------------------------
# Function: getPass
# Description: Read a line of input while showing '*'
# on the screen.
# parameters: prompt
# usage: myPW="$(getPass 'prompt')"
#---------------------------------------------------
function getPass() {
prompt="$1 "
# Loop to get all characters of the password
while IFS= read -p "$prompt" -r -s -n 1 char
do
# Check if the return key was pressed
if [[ $char == $'\0' ]]; then
break
fi
# Check to see if backspace was pressed
if [[ $char == $'\177' ]]; then
l=${#password} # length of password
# if l is > 0 then delete last char
# else do not remove any character
if [[ l -gt 0 ]]
then
prompt=$'\b \b'
password="${password%?}"
else
prompt=$''
fi
else
# Add new char to password
prompt='*'
password+="$char"
fi
done
# Echo out password to be able to capture
echo "$password"
}
#---------------------------------------------------
# End getPass
#---------------------------------------------------
#---------------------------------------------------
# End Function Library
#---------------------------------------------------