1825793 Members
2226 Online
109687 Solutions
New Discussion

Re: User password

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

User password

Hi,

I am runing a script which prompt for user to input the password I turn off the echo so that the password doesn't echo (stty -echo), now what I want is to echo **** when user input the password, can some one help how to achive this.

Thanks in advance
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: User password

Reading one character at a time from the shell is a far from trivial exercise. It can be done with dd but it is a kludge. A better approach is a small C program that will, in addition, disable echo'ing and will make sure that the terminal is not left in a noecho state. If you do a stty -noecho in a shell is is very important that you trap SIGINT,SIGHUP, and exits and restore the terminal because the stty command affects no just the process but the terminal itself. ie, if you exit the process the terminal is still in a noecho state.

Use the attached small program. It's done in K&R C intentionally so that even the Bundled compiler willl handle it. It also filters out unprintable characters.

Compile it like this:
cc onechar.c -o onechar

Now install onechar somewhere in your PATH (e.g. /usr/local/bin).

You can invoke it as "onechar -u" for usage.

Use it like this:

#!/usr/bin/sh

typeset -i STAT=0
typeset PW=""
typeset X=""
echo "Enter password: \c"
while [[ ${STAT} -eq 0 ]]
do
X=$(onechar)
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then # printable character read
echo "*\c"
PW="${PW}${X}"
fi
done
echo

echo "Password = \"${PW}\""
If it ain't broke, I can fix that.
Anthony khan
Frequent Advisor

Re: User password

I am getting the following errors.

./test1: typeset: not found
./test1: typeset: not found
./test1: typeset: not found
Enter password: ./test1: syntax error at line 10: `X=' unexpected
A. Clay Stephenson
Acclaimed Contributor

Re: User password

typeset is a shell built-in for POSIX, Korn, andn Bash -- so what shell are you running?
If it ain't broke, I can fix that.
Anthony khan
Frequent Advisor

Re: User password

Thanks Clay I was using ksh, it's working now.