1834608 Members
2754 Online
110069 Solutions
New Discussion

Re: silent read

 
SOLVED
Go to solution
cryptmonkey
New Member

silent read

I'm writing a shell script,and I want to accept a password from the user, but I don't want the echo output to screen. (as READ does).
I'm being lazy as I could write the script in PERL or C but I'd prefer to KISS and do it as a UNIX shell.
3 REPLIES 3
Rodney Hills
Honored Contributor
Solution

Re: silent read

stty -echo
read x
stty echo

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: silent read

While all you need to do is a stty -echo, it is very important that the terminal not be left in this state (process dies, or n'rupt before doing a stty echo). You should always have a trap to reset the terminal because the tty settings affect not just this process but the terminal device itself.

The better way is:

#!/usr/bin/sh

echo "Enter password: \c"
trap 'stty echo' 0 1 2 15
stty -echo
read PW
stty echo


Now no matter if you enter a Ctrl-C, for example, the terminal will be restored to sane vales. I suggest the you omit the trap and Cntl-C the password entry to see what I am talking about.
------------------------------
If it ain't broke, I can fix that.
cryptmonkey
New Member

Re: silent read

thanks to Rodney Hills & A. Clay Stephenson

stty -echo
read x
stty echo

or

echo "Enter password: \c"
trap 'stty echo' 0 1 2 15
stty -echo
read PW
stty echo

Oh and my good friend Leigh