how to display password as * in the console | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

how to display password as * in the console

Shell Programming and Scripting


Tags
shell script, shell scripting, unix scripting, unix scripting basics

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-19-2008
codeman007 codeman007 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 4 February 2011, 6:43 AM EST
Location: US
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
how to display password as * in the console

Hi,
I have to read the password input and display it as * in the console

How can I do this??
Sponsored Links
    #2  
Old 12-19-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 25 October 2014, 1:14 AM EDT
Location: /dev/ph
Posts: 4,823
Thanks: 64
Thanked 431 Times in 399 Posts
This works for ksh93 and should be easily modifable for bash.

Code:
passwd=""
omodes=`stty -g`

echo "Enter Password: \c"
while :
do
   stty raw
   c=$(dd bs=1 count=1 2>/dev/null)
   stty -raw

   # break out of loop if CR found
   [[ -z $(echo $c | tr -d "\015") ]] && break

   stty echo
   echo "*\c"
   passwd=${passwd}${c}
   stty -echo
done

stty $omodes

echo
echo "Password entered: $passwd"

Sponsored Links
    #3  
Old 12-19-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 25 October 2014, 1:14 AM EDT
Location: /dev/ph
Posts: 4,823
Thanks: 64
Thanked 431 Times in 399 Posts
For bash, the only change needed is to explicitly tell the build-in echo command to interpret "\c" correctly i.e.

Code:
passwd=""
omodes=`stty -g`

echo -e  "Enter Password: \c"
while :
do
   stty raw
   c=$(dd bs=1 count=1 2>/dev/null)
   stty -raw

   # break out of loop if CR found
   [[ -z $(echo $c | tr -d "\015") ]] && break

   stty echo
   echo -e "*\c"
   passwd=${passwd}${c}
   stty -echo
done

stty $omodes

echo
echo "Password entered: $passwd"

I love the idiosyncrasies of the echo command!
    #4  
Old 12-19-2008
crs6785 crs6785 is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 11 January 2009, 4:12 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
if your looking for a simple solution to just hide the password, and you're using bash, you could just use "read -s" ...


Code:
#!/bin/bash

echo -n "Enter a Password: " 
read -s pass_value
echo ""
echo "Password Entered: $pass_value"

Sponsored Links
    #5  
Old 12-21-2008
codeman007 codeman007 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 4 February 2011, 6:43 AM EST
Location: US
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for all the replies.
In bash when i type the password it prints the first character on screen
and then types * correctly....but the input is correct

How can i overcome this??
Sponsored Links
    #6  
Old 12-21-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 October 2014, 3:06 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
My script is here: http://www.unix.com/shell-programmin...character.html but it is ksh.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Command display output on console and simultaneously save the command and its output satimis UNIX for Dummies Questions & Answers 7 01-25-2009 07:27 PM
password verification verify password of a user for only first 8 characters amitpansuria UNIX for Advanced & Expert Users 12 10-23-2008 10:03 AM
console '# ' with {PWD} ?? gfca UNIX for Advanced & Expert Users 2 08-14-2008 07:08 PM
Change password by pushing encrypted password to systems benq70 UNIX for Dummies Questions & Answers 1 09-02-2005 09:08 AM
Console Log kdreaves UNIX for Dummies Questions & Answers 4 05-21-2004 05:13 PM



All times are GMT -4. The time now is 05:28 AM.