zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    MVC设置默认页面
    MySQL_DBA整理
    解决git提交敏感信息(回退git版本库到某一个commit)
    并发数计算
    高并发下的 Nginx 优化与负载均衡
    PassengerNginxdebian快速部署Rails
    Linux+postfix+extmail+dovecot打造基于web页面的邮件系统
    2018.11.30软件更新公告
    2018.10.11软件更新公告
    2018.09.25软件更新公告
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15017975.html
Copyright © 2011-2022 走看看