GNU Linux shell时不进行回显(输入密码时不显示的需求)
2015-11-07 12:00
591 查看
/[b]*****************************[/b]
* Author : Samson
* Date : 11/07/2015
* Test platform:
* gcc 4.8.4
* GNU bash, 4.3.30
* Debian GNU/Linux 8
* [b]***************************[/b]/
需求:
在编写bash脚本时,需要用户输入一个密码,要保证用户输入的密码不会被别人看到;
如何解决:
有两种方法:
1、使用ssty进行无回显的参数的配置开关;
2、使用read -s 进行无回显的读取;
例子:假如密码为12345678
方法一:
$ cat sttynoecho.sh
#!/bin/bash
echo "Enter password:"
stty -echo
read passwd
stty echo
echo $passwd
执行结果:
$ source sttynoecho.sh
Enter password:
12345678
方法二:
$ cat readnoecho.sh
#!/bin/bash
echo "Enter password:"
read -s passwd
echo $passwd
执行结果:
$ source readnoecho.sh
Enter password:
12345678
438
* Author : Samson
* Date : 11/07/2015
* Test platform:
* gcc 4.8.4
* GNU bash, 4.3.30
* Debian GNU/Linux 8
* [b]***************************[/b]/
需求:
在编写bash脚本时,需要用户输入一个密码,要保证用户输入的密码不会被别人看到;
如何解决:
有两种方法:
1、使用ssty进行无回显的参数的配置开关;
2、使用read -s 进行无回显的读取;
例子:假如密码为12345678
方法一:
$ cat sttynoecho.sh
#!/bin/bash
echo "Enter password:"
stty -echo
read passwd
stty echo
echo $passwd
执行结果:
$ source sttynoecho.sh
Enter password:
12345678
方法二:
$ cat readnoecho.sh
#!/bin/bash
echo "Enter password:"
read -s passwd
echo $passwd
执行结果:
$ source readnoecho.sh
Enter password:
12345678
438
相关文章推荐
- sh,bash,csh,tcsh等shell的区别
- vb.net如何判断shell完成,或者等待shell完成
- bash shell 之变量笔记
- linux shell 编程6 tomcat的启动停止
- 几个有用的Shell别名和函数
- 在windows下用XShell 连接虚拟机linux Ubuntu14,以及静态ip,DNS设置
- Linux 下 shell 编写 计算器
- 《Linux命令、编辑器与Shell编程》读书笔记4.1-查找和筛选工具(grep,find,sed)
- linux笔记:shell基础和bash的基本功能
- linux shell(2)
- shell脚本操作mysql数据库
- php调用shell脚本(web端和客户端同时实现)
- /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc
- Linux shell(1)
- shell必懂知识点
- Bash特性总结
- 将文件内容逐行读取处理并写入对应文件中
- linux vim .bashrc 配置以及一些常用的linux 命令解析
- 批量执行shell命令
- 批量执行shell命令