Reading password and echo * character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading password and echo * character
# 1  
Old 01-14-2008
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
This User Gave Thanks to alanpachuau For This Post:
# 2  
Old 01-14-2008
It's a lot of trouble....
Code:
#! /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=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.
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 01-14-2008
Thank you for the code...it is really helpful
Thaks once again
# 4  
Old 04-06-2008
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 ?
# 5  
Old 04-06-2008
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

2. Shell Programming and Scripting

Problem with character by character reading

Hi friend, i have the following problem: when i am writting the below command on the command prompt , its working. while read -n 1 ch; do echo "$ch" ; echo "$ch" ; done<file_name.out. but when i am executing it after saving it in a ksh file, its not working. Please helppppppppp .. thankss... (18 Replies)
Discussion started by: neelmani
18 Replies

3. Shell Programming and Scripting

File Reading for a certain string and echo the line before

Hi Guys, I have a big file like this. It has cache group line ( the bold lines ) and then followed by 10 status lines showing either Compelte or Failed. This pattern repeats several time. We can assume all the status lines have either Complete or Failed status for that Cache Group line. I... (13 Replies)
Discussion started by: MKNENI
13 Replies

4. UNIX for Dummies Questions & Answers

Reading character by character - BASH

Hello every one and thanks in advance for the time you will take to think about my problem. I would like to know if it's possible (in BASH) to read a text file character after character. Exactly this is what I would like to do : Txt file : ATGCAGTTCATTGCCAAA...... (~2.5 millions... (3 Replies)
Discussion started by: sluvah
3 Replies

5. Shell Programming and Scripting

help with echo command in reading blank spaces in aix

Hi, I have a test.txt and would like to put in a different file but while putting in the other file, blank spaces are missing. $ indicates blank spaces filename: test.txt: USA$$$840$$$$ Desired output USA$$$840$$$$ Current output as per the following code. while read j; do... (3 Replies)
Discussion started by: techmoris
3 Replies

6. Shell Programming and Scripting

Echo password

hi all, I am using HP-UX I want to show password with * instead of space on screen while i entered password through keyboard. can you tell me that shell script code? i used following code stty -echo read pass stty echo echo $pass but this time it wont show anything on... (5 Replies)
Discussion started by: hari_uctech
5 Replies

7. HP-UX

reading password and echoing '*' character on console

hi all, I am using HP-UX system. I want echoing * characters while reading password through keyboard instead of blank space. can u help me for that code? Thanks (1 Reply)
Discussion started by: hari_uctech
1 Replies

8. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

9. UNIX for Dummies Questions & Answers

Weird character in between echo function

Hi All, Appreciate if anyone can help. I've a script where it does echo function like this while do FILE_ARG="cu0${w}_${FILE}_${DT}.av" ORACLE_ERROR=`grep "ORA-" ${FILE_ARG}` if ]; then Func_Log_Writer "Fail! ${FILE_ARG}\n" Func_Log_Writer "Error message:... (2 Replies)
Discussion started by: agathaeleanor
2 Replies

10. Shell Programming and Scripting

suppress echo while reading input in expect

Hi all, I tried searching for similar threads. Couldn't get any for Expect scripting. I am trying automate a task which requires login to a machine. The user would be prompted for the password. Now, I want this input to be hidden from being displayed on the screen. Is there... (1 Reply)
Discussion started by: sudhir_onweb
1 Replies
Login or Register to Ask a Question