Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Password characters 2

Status
Not open for further replies.

scriptfinder

Technical User
Apr 17, 2006
18
US

Please someone help me find a way to solve password character issue.
I have 2 input statements for the user, one of which is a password to be entered. When the user inputs the password on the screen, I would like the characters to appear as asterisks or stars. I can use stty to make the characters disappear but I was wondering if I can print some special characters instead of blanks.
Thank you in advance and you guys really rock!

 
That isn't straightforward. What shell and OS are you using?

In bash you could put a while loop around read -n1 commands to read each individual character and display an appropriate character on screen, but you would have to handle cursor positioning manually for backspacing, etc.

Annihilannic.
 
Sorry for getting back to you late and thanks for the response.
The OS is SunOs and the shell is ksh.
BTW, read -n1 came out as a bad option error.

In a nutshell, the user should be able to see some characters while entering the password. The user is going to run a very long script in which entering a password is in the middle and if the user fails to enter the right password, the process has to be started all the way from the beginning. I know that wrong password will result in rerun but if I can at least help the user see some chars being entered I can avoid a possible rerun of the script.

Your help is appreciated.
 
It's not particularly pretty, but this code snippet seems to do it. Let me know if you have any questions.

Code:
#!/bin/ksh

readkey() {
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
dd bs=1 count=1 <&0 2>/dev/null
stty $oldstty
}

row=10
col=17
tput clear
tput cup $row 1
echo "Enter Password:"
tput cup $row $col
while true
do
   mychar=$(readkey)
   # break for the CR key
   if [[ -z $mychar ]]
   then
      break
   fi
   echo "*\c"
   mypassword="$mypassword"$mychar
done
((row+=11))
tput cup $row 10
echo $mypassword
 
Nice work Ed. Here are some modifications to handle backspacing, note that the backspace character is hard-coded though and must be entered in vi using "Ctrl-V, Backspace":

Code:
#!/bin/ksh

readkey() {
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
dd bs=1 count=1 <&0 2>/dev/null
stty $oldstty
}

row=10
col=17
tput clear
tput cup $row 1
echo "Enter Password:"
tput cup $row $col
while true
do
   mychar=$(readkey)
   # break for the CR key
   if [[ -z $mychar ]]
   then
      break
   fi
   if [[ "$mychar" = "^?" && ${#mypassword} -gt 0 ]]
   then
      tput cub1 ; echo " \c" ; tput cub1
      mypassword="${mypassword%?}"
   else
      echo "*\c"
      mypassword="$mypassword"$mychar
   fi
done
((row+=11))
tput cup $row 10
echo $mypassword

Annihilannic.
 
Correction for correct handling of backspace at the beginning of the field:

Code:
...
   if [[ "$mychar" = " ]]
   then
      if [[ ${#mypassword} -gt 0 ]]
      then
         tput cub1 ; echo " \c" ; tput cub1
         mypassword="${mypassword%?}"
      fi
...

Annihilannic.
 
You guys are great! it works fine except that backspaces appear as chars and the spacebar acts as backspace. Shouldn't it be the other way? Any ideas?

Also, I just made one minor correction - a closing double quote after "$mychar" = " ". Am I right?
 
Thanks, Annihilannic. Good hearing from you.

Those pesky special characters always get in the way. It's really a case of how your keyboards are mapped.
 
scriptfinder, if you do an stty -a what is shown as the "erase" character?

When you entered or copied/pasted the script, did you use Ctrl-V, backspace for the "^?" part instead of just typing a "^" and a "?"?

Annihilannic.
 

I just now did what you mentioned and no issues now.

You guys are too good!


 
Hi guys,

what this doesn't work in linux :

The result it :
Code:
Enter Password: stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor
*stty: standard input: Bad file descriptor

haitest

why they keep complaining about "Bad file descriptor" ?
 
Works fine for me on RHEL AS2.1, AS 3.0 and SLES9. What version of ksh or pdksh are you using?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top