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
shell脚本(4)-格式化输入的更多相关文章
- Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载
Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...
- Shell 脚本处理用户输入
传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户的输入 bash shell提供了一些不同的方法来从用户处获取数据,包括命令行参数(添加在命令后数据),命令行选项(可以修改命令行为的单个 ...
- shell脚本(3)-格式化输出
一个程序需要有0个或以上的输入,一个或更多输出 一.echo语法 1.功能:将内容输出到默认显示设备. echo命令功能在显示器上显示一段文字,一般提到提示的作用 2.语法:echo[-ne][字符串 ...
- shell脚本,计算输入给定的数,判断最大值,最小值,总和?
[root@localhost ~]# cat five.sh #!/bin/bash #任意输入5个数,判断最大值,最小值,总和 s= read -p "please input:&quo ...
- Linux shell脚本读取用户输入的参数
新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...
- shell脚本中格式化日期
date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...
- 常用shell脚本
[脚本1]打印形状打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please input the length: " n ...
- 【转】干货分享-100个shell脚本
本文用于记录学习和日常中使用过的shell脚本 [脚本1]打印形状 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please i ...
- 【转】70个经典的 Shell 脚本面试问题
我们为你的面试准备选择了 70 个你可能遇到的 shell 脚面问题及解答.了解脚本或至少知道基础知识对系统管理员来说至关重要,它也有助于你在工作环境中自动完成很多任务.在过去的几年里,我们注意到所有 ...
- Shell脚本中执行mysql的几种方式(转)
Shell脚本中执行mysql的几种方式(转) 对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用 ...
随机推荐
- [CF1748D] ConstructOR
题目描述 You are given three integers $ a $ , $ b $ , and $ d $ . Your task is to find any integer $ x $ ...
- [ABC274D] Robot Arms 2
Problem Statement You are given a sequence $A = (A_1, A_2, \dots, A_N)$ of length $N$ consisting of ...
- MySQL运维3-分库分表策略
一.介绍 单库瓶颈:如果在项目中使用的都是单MySQL服务器,则会随着互联网及移动互联网的发展,应用系统的数据量也是成指数式增长,若采用单数据库进行存储,存在一下性能瓶颈: IO瓶颈:热点数据太多,数 ...
- Python给exe添加以管理员运行的属性
需求 有些应用每次启动都需要用管理员权限运行,比如Python注入dll时,编辑器或cmd就需要以管理员权限运行,不然注入就会失败. 这篇文章用编程怎么修改配置实现打开某个软件都是使用管理员运行,就不 ...
- 使用Mybatis自定义插件实现不侵入业务的公共参数自动追加
背景 后台业务开发的过程中,往往会遇到这种场景:需要记录每条记录产生时间.修改时间.修改人及添加人,在查询时查询出来. 以往的做法通常是手动在每个业务逻辑里耦合上这么一块代码,也有更优雅一点的做法是写 ...
- MyBatis 批量更新的处理
一般来讲,在使用 MyBatis 进行数据库的访问时,通常会遇到需要更新数据的相关业务,在某些业务场景下,如果需要进行一批次的数据更新,可能性能不是特别理想.本文将简要介绍几种能够高效地处理批量更新数 ...
- IO流小结图片
- Java 中常见类型的判空方式
引用类型(Reference Types): 使用 == 运算符判断是否为 null. 使用 != 运算符判断是否不为 null. 使用 Objects.isNull() 方法判断是否为 null. ...
- C# 将Excel转为OFD、UOS
本文以C#及VB.NET代码为例展示如何将Excel工作簿转换为OFD和UOS格式.通过workbook.LoadFromFile(string fileName)方法加载Excel源文档后,然后调用 ...
- 神经网络入门篇:详解为什么需要非线性激活函数?(why need a nonlinear activation function?)
为什么需要非线性激活函数? 为什么神经网络需要非线性激活函数?事实证明:要让的神经网络能够计算出有趣的函数,必须使用非线性激活函数,证明如下: 这是神经网络正向传播的方程,现在去掉函数\(g\),然后 ...