Unix 如何关闭终端中的回声?
我正在编写一个Bourne shell脚本,密码输入如下:Unix 如何关闭终端中的回声?,unix,terminal,sh,Unix,Terminal,Sh,我正在编写一个Bourne shell脚本,密码输入如下: echo -n 'Password: ' read password 显然,我不希望密码被回显到终端,所以我想在读取期间关闭回显。我知道有办法用stty做到这一点,但为了社区的利益,我会在阅读手册时问这个问题 read-s password适用于我的linux机器。Bourne Shell脚本: #!/bin/sh # Prompt user for Password echo -n 'Password: ' # Do not s
echo -n 'Password: '
read password
显然,我不希望密码被回显到终端,所以我想在读取期间关闭回显。我知道有办法用
stty
做到这一点,但为了社区的利益,我会在阅读手册时问这个问题 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
@:/dir #man 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
stty手动命令了解更多信息:
#!/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
@:/dir #man 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
stty手册片段:
#!/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
@:/dir #man 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
如果您想从终端隐藏打印,也可以使用“stty-echo”。并使用“stty echo”恢复终端设置
但是我认为从用户那里获取密码输入“read-s password”已经足够了。如果内存可用的话,这肯定是我以前做过的。出于好奇,这和stty-echo有什么不同吗;读福;stty echo?是的,如果echo已经关闭,它会在之后关闭。@ak2:谢谢,这很有道理。我是unix新手。当您刚接触unix@AbhishekGupta:1时,您能解释一下stty到底是什么以及这段代码做了什么更一般,比Wikipedia更好。了解如何滚动、搜索和退出
less
(或more
)。2. man whateveryouwonderabout
或者,对于bash内置命令,如“set”,“help set”。祝你旅途好运;-)这是有用的信息,但-s似乎是一个Bash扩展,因为read-sfoo
在Dash(Ubuntu符号链接到/bin/sh
的Bourne shell)中生成read:1:非法选项-s
。可能是stty的重复,而不是ssty。