/ Shell

Shell中实现输入密码

不显示字符:

print -n "Enter password - " stty -echo read PASSWORD stty echo echo "" print $PASSWORD

显示*号

function savetty { SAVETTY=$(stty -g </dev/tty) } function hidestty { stty raw -echo </dev/tty } function resetstty { stty ${SAVETTY:-cooked} </dev/tty stty echo </dev/tty } function getpass { typeset ans passwd cr=$(print -n "\r") bs=$(print -n "\b") typeset vname=${1:-PASSWORD} savetty hidestty ans= passwd= while : do ans=dd if=/dev/tty count=1 bs=1 2>/dev/null case $ans in $bs) ((${#passwd})) && { print -n "\b \b" passwd=${passwd%?} } ;; $cr) break ;; [[:print:]]) print -n '*' passwd="${passwd}${ans}" esac done eval $vname='$passwd' } getpass PASSWD resetstty echo "" echo "$PASSWD" read PASSWD echo "$PASSWD"

转自http://www.dewen.org/q/2790

Shell中实现输入密码
Share this