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

     

  • 相关阅读:
    阿里知识图谱首次曝光:每天千万级拦截量,亿级别全量智能审核
    LSTM简介以及数学推导(FULL BPTT)
    深度学习算法索引(持续更新)
    学界 | Yann LeCun新作,中日韩文本分类到底要用哪种编码?
    Android 常见内存泄漏的解决方式
    集成支付宝支付
    【4.29安恒杯】writeup
    sdut 3-7 类的友元函数的应用
    Linux下libsvm的安装及简单练习
    iOS 使用腾讯地图显示用户位置注意事项
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15017975.html
Copyright © 2011-2022 走看看