The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
suppress echo while reading input in expect sudhir_onweb Shell Programming and Scripting 1 07-23-2007 10:54 AM
Script for Reading a Password file srirams Shell Programming and Scripting 2 03-07-2007 10:20 AM
ASCII character to accept logon password gord UNIX for Dummies Questions & Answers 8 02-02-2006 03:58 PM
How to echo password characters mukluk UNIX for Dummies Questions & Answers 4 09-12-2005 08:34 AM
reading a single character in C alodha High Level Programming 3 01-28-2005 06:22 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-14-2008
alanpachuau alanpachuau is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
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
  #2 (permalink)  
Old 01-14-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
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.
  #3 (permalink)  
Old 01-14-2008
alanpachuau alanpachuau is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
Thank you for the code...it is really helpful
Thaks once again
  #4 (permalink)  
Old 04-06-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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 (permalink)  
Old 04-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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).
Closed Thread

Bookmarks

Tags
keystroke

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0