您现在的位置是:网站首页> 编程资料编程资料

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

2023-05-26 371人已围观

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

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

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

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

 #!/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”

-六神源码网