shell脚本(4)-格式化输入

一、read命令

1、概念:

默认接受键盘的输入,回车符代表输入结束

2、read命令选项

-p:打印信息

-t:限定时间

-s:不回显

-n:输入字符个数

3、举例说明

(1)模拟登录

[root@localhost test20210724]# vi read_command_study.sh

#!usr/bin/bash
clear
echo -n -e "Login: "
read acc
echo -n -e "Password: "
read pw
echo "account:$acc password:$pw"

查看运行结果:

[root@localhost test20210724]# sh read_command_study.sh 
Login: root
Password: 123
account:root password:123

(2)优化:read -s  #不显示密码

[root@localhost test20210724]# vi read_command_study.sh 

#!usr/bin/bash
clear
echo -n -e "Login: "
read acc
echo -n -e "Password: "
read -s pw
echo echo
"account:$acc password:$pw"

查看运行结果:

[root@localhost test20210724]# sh read_command_study.sh 
Login: root
Password: 
account:root password:123

(3)优化:read -t5 #增加5秒超时,5秒不输入退出

[root@localhost test20210724]# vi read_command_study.sh 

#!usr/bin/bash
clear
echo -n -e "Login: "
read acc
echo -n -e "Password: "
read -s -t5  pw
echo
echo "account:$acc password:$pw"

查看运行结果:

[root@localhost test20210724]# sh read_command_study.sh 
Login: root
Password: 
account:root password:

(4)优化:read -n6 #密码只识别6位,超过6位自动输出完成

[root@localhost test20210724]# vi read_command_study.sh 

#!usr/bin/bash
clear
echo -n -e "Login: "
read acc
echo -n -e "Password: "
read -s -t5 -n6 pw
echo
echo "account:$acc password:$pw"

查看运行结果:

[root@localhost test20210724]# sh read_command_study.sh 
Login: root
Password: 
account:root password:123456

(5)优化:read -p "Login: " acc #read并且打印输出

[root@localhost test20210724]# vi read_command_study.sh 

#!usr/bin/bash
clear
read -p "Login: " acc
read -s -t5 -n6 -p "Password: " pw
echo
echo "account:$acc password:$pw"

查看运行结果:

[root@localhost test20210724]# sh read_command_study.sh 
Login: root
Password: 
account:root password:123

 

shell脚本(4)-格式化输入

上一篇:Photoshop打造漂亮的彩色发光圆环


下一篇:GPGPU-Sim研究(一)-深入掌握GPGPU的工作原理