shell中输入内容隐形-read -s + stty

系统+安全 Liemer_Lius 618℃

1、通过read -s实现

read -s是将输入内容隐形的一个方法,常用户密码输入等,类似ssh免密输入密码等,示例如下:

> cat /tmp/a.sh 
#!/bin/bash
read -s -p "Please input something: " STH
echo  # 不echo一个空行,页面布局会变乱
echo "Your input is: $STH"

> sh /tmp/a.sh
Please input something: 
Your input is: Liemer Lius   # 加了echo之后另起一行

> cat /tmp/a.sh 
#!/bin/bash
read -s -p "Please input something: " STH
#echo  # 不echo一个空行,页面布局会变乱
echo "Your input is: $STH"

> sh /tmp/a.sh
Please input something: Your input is: Liemer_lius   # 两行合并,没有另起一行

 

2、通过stty实现

stty是终端级别的输入隐藏,键入内容不再在终端显示,示例如下:

#!/bin/bash
stty -echo
read -p "Please input STH: " STH
echo
echo "You have input ${STH}"
stty echo
$ sh lius.sh
Please input STH:     # 输入内容隐藏了
You have input helldldlddl

 

 

转载请注明:liutianfeng.com » shell中输入内容隐形-read -s + stty

喜欢 (13)

评论已关闭。