一、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)-格式化输入的更多相关文章

  1. Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

    Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载   这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...

  2. Shell 脚本处理用户输入

    传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户的输入 bash shell提供了一些不同的方法来从用户处获取数据,包括命令行参数(添加在命令后数据),命令行选项(可以修改命令行为的单个 ...

  3. shell脚本(3)-格式化输出

    一个程序需要有0个或以上的输入,一个或更多输出 一.echo语法 1.功能:将内容输出到默认显示设备. echo命令功能在显示器上显示一段文字,一般提到提示的作用 2.语法:echo[-ne][字符串 ...

  4. shell脚本,计算输入给定的数,判断最大值,最小值,总和?

    [root@localhost ~]# cat five.sh #!/bin/bash #任意输入5个数,判断最大值,最小值,总和 s= read -p "please input:&quo ...

  5. Linux shell脚本读取用户输入的参数

    新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...

  6. shell脚本中格式化日期

    date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...

  7. 常用shell脚本

    [脚本1]打印形状打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please input the length: " n ...

  8. 【转】干货分享-100个shell脚本

    本文用于记录学习和日常中使用过的shell脚本 [脚本1]打印形状 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please i ...

  9. 【转】70个经典的 Shell 脚本面试问题

    我们为你的面试准备选择了 70 个你可能遇到的 shell 脚面问题及解答.了解脚本或至少知道基础知识对系统管理员来说至关重要,它也有助于你在工作环境中自动完成很多任务.在过去的几年里,我们注意到所有 ...

  10. Shell脚本中执行mysql的几种方式(转)

    Shell脚本中执行mysql的几种方式(转) 对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用 ...

随机推荐

  1. [ABC278G] Generalized Subtraction Game

    Problem Statement This is an interactive task (where your program interacts with the judge's program ...

  2. [ABC248G] GCD cost on the tree

    Problem Statement You are given an undirected tree with $N$ vertices. Let us call the vertices Verte ...

  3. Odoo接口说明

    通过使用PostMan调用Odoo - POST { "jsonrpc":"2.0", "params":{ "db": ...

  4. STM32CubeMX教程8 TIM 通用定时器 - 输出比较

    1.准备材料 开发板(STM32F407G-DISC1) ST-LINK/V2驱动 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) 逻 ...

  5. Not on FX application thread(八)

    Not on FX application thread(八) JavaFX 从入门到入土系列 当你不在主线程中操作UI时会出现以下异常: Not on FX application thread 可 ...

  6. MyBatis 的缓存处理

    作为常见的 ORM 框架,在使用 MyBatis 的过程中可以针对相关的查询进行缓存处理以提高查询的性能.本文将简要介绍一下 MyBatis 中默认的一级缓存和二级缓存,以及自定义缓存的处理 MyBa ...

  7. Java 新技术:虚拟线程使用指南(二)

    虚拟线程是在 Java 21 版本中实现的一种轻量级线程.它由 JVM 进行创建以及管理.虚拟线程和传统线程(我们称之为平台线程)之间的主要区别在于,我们可以轻松地在一个 Java 程序中运行大量.甚 ...

  8. 使用推测解码 (Speculative Decoding) 使 Whisper 实现 2 倍的推理加速

    Open AI 推出的 Whisper 是一个通用语音转录模型,在各种基准和音频条件下都取得了非常棒的结果.最新的 large-v3 模型登顶了 OpenASR 排行榜,被评为最佳的开源英语语音转录模 ...

  9. dart的语法

    dart的语法 main方法 main(){ print("有返回值"); } void main() { print("没有返回值"); } 字符串的定义的方 ...

  10. Spring系列:基于注解的方式构建IOC

    目录 一.搭建子模块spring6-ioc-annotation 二.添加配置类 三.使用注解定义 Bean 四.@Autowired注入 五.@Resource注入 六.全部代码 从 Java 5 ...