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

     

  • 相关阅读:
    第3章 对象基础
    [置顶] CSDN博客客户端(非官方)
    javascript 修改对象
    Print2Flash出现"System Error. Code:1722. RPC服务器不可用."错误解决办法
    ConfigHelper 配置文件辅助类
    多个委托方法的顺序执行
    javascript Table
    字符串拼接方式(待商榷)
    CSDN博客客户端(非官方)
    javascript 对象继承
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15017975.html
Copyright © 2011-2022 走看看