我正在编写一个Bourne shell脚本,并input如下的密码:
echo -n 'Password: ' read password
显然,我不希望密码被回显到terminal,所以我想在阅读期间closures回声。 我知道有办法用stty
做这个,但是我会在阅读manpage的时候问这个问题。 ;)
stty_orig=`stty -g` stty -echo echo 'hidden section' stty $stty_orig
read -s password
在我的linux机器上工作。
Bourne Shell脚本:
#!/bin/sh # Prompt user for Password echo -n 'Password: ' # Do not show what is being typed in console by user stty -echo # Get input from user and assign input to variable password read password # Show what is being typed in console stty echo
stty手动命令了解更多信息:
@:/dir #man stty
stty手动片段:
STTY(1) stty 5.2.1 (March 2004) STTY(1) NAME stty - change and print terminal line settings SYNOPSIS stty [-F DEVICE] [--file=DEVICE] [SETTING]... stty [-F DEVICE] [--file=DEVICE] [-a|--all] stty [-F DEVICE] [--file=DEVICE] [-g|--save] DESCRIPTION Print or change terminal characteristics. -a, --all print all current settings in human-readable form -g, --save print all current settings in a stty-readable form -F, --file=DEVICE open and use the specified DEVICE instead of stdin --help display this help and exit --version output version information and exit Optional - before SETTING indicates negation. An * marks non-POSIX settings. The underlying system defines which settings are available. Local settings: [-]echo echo input characters
您可以使用read命令的'-s'选项来隐藏用户输入。
echo -n "Password:" read -s password if [ $password != "..." ] then exit 1; # exit as password mismatched # fi
如果你想隐藏终端打印,你也可以使用“ssty -echo” 。 并使用“ssty echo”恢复终端设置
但我认为从用户的“读取密码”获取密码是绰绰有余的。