Reading password and echo * character

Hi,

First of all i am using solaris 10. I want to write a script that ask user to enter password and read the character input from keyboard. The ask to re-enter the password and then if they are match it will accept.

But my problem is I want to echo a '*' character instead of the character I entered from keyboard, Please help me with this....

I know how to hide by turning off echo as 'stty -echo' but i want to display '*' character. Will it be possible?

Please help

Thank you
Alan

1 Like

It's a lot of trouble....

#! /usr/bin/ksh

exec 4>/dev/tty

function getpass
{
        typeset prompt=$1
        typeset backspace=$(echo  \\b\\c)
        typeset enter=$(echo \\r\\c)
        typeset savesetting=$(stty -g)
        typeset keystroke password n i reading result
        n=0
        echo "${prompt}"\\c >&4
        stty -echo -icrnl -icanon min 1 time 0
        reading=1
        while ((reading)) ; do
                keystroke=$(dd bs=1 count=1 2>/dev/null)
                case $keystroke in
                $enter)
                        reading=0
                        ;;
                $backspace)
                        if ((n)) ; then
                                echo "${backspace} ${backspace}"\\c >&4
                                ((n=n-1))
                        fi
                        ;;
                *)
                        echo \*\\c >&4
                        data[n]=$keystroke
                        ((n=n+1))
                esac
        done
        stty "$savesetting"
        echo >&4
        result=""
        i=0
        while  ((i<n)) ; do
                result="${result}${data}"
                ((i=i+1))
        done
        echo $result
        return 0
}

final=$(getpass "password -  ")
echo the password is $final
exit 0

The allows the backspace key to correct a typo. Remove the backspace stuff from the script if you want to allow a backspace in a password.

1 Like

Thank you for the code...it is really helpful
Thaks once again

Perderabo

In your script the key combination for backspace is \\b\\c. How did you get that ? And how do you go about getting the key combination for other characters ?

The \c means don't print a newline (in some versions of echo) and \b is backspace. Read the documentation for your echo (or maybe printf) to see the available escape codes. Basically it's just ASCII anyway. The backslashes are doubled because the shell eats one level (or you could use single quotes).