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

     

  • 相关阅读:
    HDU1506 Largest Rectangle in a Histogram(算竞进阶习题)
    洛谷P1073 最优贸易
    CH2101 可达性统计(算竞进阶习题)
    BZOJ1012 最大数maxnumber
    POJ 3764 The XOR Longest Path
    洛谷P4513 小白逛公园
    外边距叠加问题
    读JS高性能总结——DOM编程(一)
    DOM修改元素的方法总结
    DOM查找元素的方法总结
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15017975.html
Copyright © 2011-2022 走看看