脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - Shell脚本中实现把输入的密码转换为*(星号)的方法

Shell脚本中实现把输入的密码转换为*(星号)的方法

2023-02-14 11:30shell教程网 shell

这篇文章主要介绍了Shell脚本中实现把输入的密码转换为*(星号)的方法,从而把密码字符串隐藏起来,比较实用的一个功能,在做交互设计时非常有用哦,需要的朋友可以参考下

如果你需要写一段与用户交互,且需要输入一些敏感信息的(例如:用户密码、License等),那么直接用printf+read的方式,就会把用户输入的信息显示在屏幕了,这是不符合信息安全的,而且对客户体验来说也显得不够专业,所以就需要将用户输入的密码转换为*,样式如下:

please input your passwd:1234
修改为:
please input your passwd:****

那么具体如何实现呢,请往下看……

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
 
 
getchar() {
 
  stty cbreak -echo
 
  dd if=/dev/tty bs=1 count=1 2> /dev/null
 
  stty -cbreak echo
 
}
 
 
printf “Please input your passwd: ”
 
 
while : ; do
 
  ret=`getchar`
 
  if [ x$ret = x ]; then
 
    echo
 
    break
 
  fi
 
  str=”$str$ret”
 
  printf “*”
 
done
 
 
echo “Your password is: $str”

延伸 · 阅读

精彩推荐