Read stdin, no echo

Read stdin, no echo

Post by Stephane CHAZEL » Tue, 04 Nov 1997 04:00:00




>Is there a way in sh/bash to read data from stdin without echoing to the
>screen (as in a password)?

Yes, you must tell your terminal not to echo characters.

stty -help
[...]
  [-]echo       echo input characters
[...]

Then, you can inclose your reading command between a "stty -echo" and a "stty
echo" as in :

stty -echo ; read var ; stty echo

or

stty -echo ; cat > file ; stty echo

The same can be done with other shells.

--
Stephane

 
 
 

Read stdin, no echo

Post by Sean Hardi » Tue, 04 Nov 1997 04:00:00


Is there a way in sh/bash to read data from stdin without echoing to the
screen (as in a password)?

TIA

sean

 
 
 

Read stdin, no echo

Post by Suresh Kalla » Tue, 04 Nov 1997 04:00:00


Hi Sean ,

Try this

=====================
#!/bin/sh
echo " Enter your name ... But it will not be  echoed"
stty -echo  #### To prevent form echoing to the screen
read var
stty echo  #### To bring it back to earlier stae.
echo " You entered $var"
=====================

Hope this helps.

Suresh S Kallaje
Systems Engineer
WIPRO SYSTEMS
INDIA

: Is there a way in sh/bash to read data from stdin without echoing to the
: screen (as in a password)?

: TIA

: sean

 
 
 

Read stdin, no echo

Post by Bill Marc » Tue, 04 Nov 1997 04:00:00




Quote:>Is there a way in sh/bash to read data from stdin without echoing to the
>screen (as in a password)?

stty -echo

--
Bill Marcum
"Avoid making eye contact with the factory people. They can see
directly into your soul."

 
 
 

Read stdin, no echo

Post by Stephen Mulcah » Thu, 06 Nov 1997 04:00:00




: >Is there a way in sh/bash to read data from stdin without echoing to the
: >screen (as in a password)?
: >
: stty -echo

On a relatded note. Is there any easy/standard way to get asterisks
echo'ed backed for each character typed without writing an elaborate c
program to do so?

-stephen

 
 
 

Read stdin, no echo

Post by Fred Su » Fri, 07 Nov 1997 04:00:00



Quote:> Is there any easy/standard way to get asterisks
> echo'ed backed for each character typed without writing an elaborate c
> program to do so?

I whipped up this in Bash.  It could definitely be improved for ksh (by
using '[[ ]]' instead of '[ ]' and 'print' instead of 'echo' for improved
efficiency, for example), but the principle seems to work:

#!/bin/bash

newLine="$(echo X | tr 'X' '\015')"
crgRet="$(echo X | tr 'X' '\012')"
backSpace="$(echo X | tr 'X' '\010')"

Print='echo -e'   # enables '\r' in Bash; you may not need this in your shell

getField  ()
{
        getFieldGeneric insecure

Quote:}

getSecureField  ()
{
        getFieldGeneric secure

Quote:}

getFieldGeneric  ()
{
        typeset newChar
        typeset dispChar='*'  # change this to display some other character
        typeset secureField
        Field=

        while : ; do
                stty -echo raw
                newChar=$(dd if=/dev/tty bs=1 count=1 2>/dev/null)
                stty -raw echo
                if [ "x${newChar}x" = "x${newLine}x"  -o  \
                         "x${newChar}x" = "x${crgRet}x" ]; then
                        echo
                        break;
                elif [ "x${newChar}" = "x${backSpace}x" ]; then
                        Field="${Field%?}"
                        secureField="${secureField%?}"
                else
                        Field="${Field}${newChar}"
                        secureField="${secureField}${dispChar}"
                fi
                if [ "$1" = "secure" ]; then
                        $Print -n "\r${secureField} ${backSpace}"
                else
                        $Print -n "\r${Field} ${backSpace}"
                fi
        done

Quote:}

getSecureField
echo "getSecureField got: \"${Field}\""

getField
echo "getField got: \"${Field}\""

Give it a burl!

Fred Surr
Melbourne

 
 
 

Read stdin, no echo

Post by Stephane CHAZEL » Fri, 07 Nov 1997 04:00:00



>On a relatded note. Is there any easy/standard way to get asterisks
>echo'ed backed for each character typed without writing an elaborate c
>program to do so?

If you use zsh, you can use the option "-k" of the builtin command "read":

stty -echo
var=""
while read -k c
do if [[ "$c" == "
" ]]
   then break
   fi
   echo -n \*
   var=${var}$c
done
stty echo

Unfortunetly, it's not standard.

--
Stphane

 
 
 

1. Reading from stdin without RETURN and echo

Hi,

I'm programming in C on a Red Hat 6.2 system and I'd like to know how
can I read a single character from the keyboard without waiting for
the RETURN key? I'd also like to know how can I stop characters from
being echoed to the stdout when they're typed?

Thanks for the answer,
Rui

2. Austin (Arima) Laptop with XFree

3. Software to read NOS/VE backup tapes

4. Virtual console problem

5. read from stdin while reading line by line a file

6. HELP...... LILO

7. Echoing as stdin to /dev/???

8. Can IP_MASQUERADE work with TIA?

9. No screen echo from stdin

10. Sending data to STDIN of a process using echo

11. piped data & 'read' stdin

12. Reading a line from stdin using SCO csh

13. Reading SINGLE chars from stdin???