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脚本下调用 ...
随机推荐
- @Async实现异步任务
1.@Async是SpringBoot自带的一个执行步任务注解 @EnableAsync // 开启异步 @SpringBootApplication public class Application ...
- NetSuite 开发日记 —— 估价单(Estimate)新建项目(Project)自动填入项目字段
Background 用户在估价单(Estimate)点击「项目」字段旁边+按钮新建项目(Project),在项目的自定义字段中自动填入估价单ID 用户在项目记录存在切换自定义表格行为 Analysi ...
- 【Python微信机器人】第六七篇: 封装32位和64位Python hook框架实战打印微信日志
目录修整 目前的系列目录(后面会根据实际情况变动): 在windows11上编译python 将python注入到其他进程并运行 注入Python并使用ctypes主动调用进程内的函数和读取内存结构体 ...
- Centos8 硬盘挂载
查看硬盘分区状况 fdisk -l 可以看到数据盘 /dev/vdb 大小为 10Gb,目前还没有进行分区. 对 /dev/vdb 资源盘进行分区 fdisk /dev/vdb //根据提示输入信息: ...
- 牛客刷Java记录第6天
第一题 一个文件中的字符要写到另一个文件中,首先需要( ). A System.out.print (buffer[i]); B FileOutputStream fout = new FileOut ...
- Pikachu漏洞靶场 (SSRF服务端请求伪造)
SSRF(Server-Side Request Forgery:服务器端请求伪造) curl 点击 累了吧,来读一首诗吧 url是这样的: http://192.168.171.30/pikachu ...
- vuepress借助jenkins和svn实现文档自动化更新部署
前言 有个需求,需要将放在SVN的用vuepress写的文档进行自动化更新和部署,每次有人在本地将写好的md文件更新到svn时候,由jenkins实现自动打包来实现自动更新的功能. docker安装j ...
- 快速批量升级 NugetPackage 版本
批量升级项目中的 Nuget 有时候我们需要升级整个解决方案中的某些Nuget版本,如果每个手动使用NuGet Package Manager 会很麻烦.经过一个周末的踩坑,我找到一个解决方案. Na ...
- Llama2-Chinese项目:5-推理加速
随着大模型参数规模的不断增长,在有限的算力资源下,提升模型的推理速度逐渐变为一个重要的研究方向.常用的推理加速框架包含lmdeploy.FasterTransformer和vLLM等. 一.lmd ...
- 云小课|MRS基础原理之Oozie任务调度
阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:Oozie是一个基 ...