Linux 基础教程 45-read命令
基本用法
read命令主要用于从标准输入读取内容或从文件中读取内容,并把信息保存到变量中。其常用用法如下所示:
read [选项] [文件]
| 选项 | 解释 |
|---|---|
| -a array | 将内容读取到数值中,变量默认为数组且以空格做为分割符 |
| -d delimiter | 遇到指定的字符即停止读取 |
| -n nchars | 指定最多可以读入的字符数,即定义输入文本的长度 |
| -r | 屏蔽转义符 |
| -p prompt | 显示提示信息 |
| -s | 静默模式,在输入字符时不在终端中显示,常用于密码输入等 |
| -t timeout | 指定超时时间 |
| -u FD | 从文件描述符中读入,该FD可以由exec开启 |
用法示例
1、从标准输入读入
[root@localhost test]# cat read.sh
#!/bin/bash
echo -n "Please input your name:"
read name
echo "Hello $name"
[root@localhost test]# bash read.sh
Please input your name:Jack
Hello Jack
2、指定显示信息从标准输入读入
[root@localhost test]# cat read.sh
#!/bin/bash
# echo -n "Please input your name:"
read -p "Please input your name:" name
# read name
echo "Hello $name"
[root@localhost test]# bash read.sh
Please input your name:Jack
Hello Jack
在以上示例中,read是一次可以接受多个参数的,如下所示:
read -p "Please input your name:" firstName secondName lastName
但需要注意的事项如下:
- 如果输入的数据少于变量个数,则多余的变量不会获取到数据,即变量值为空
- 如果输入的数据多于变量个数,则超出的数据将全部赋给最后一个变量
- 如果在read命令后面没有定义任何变量,脚本在执行时,如果用户输入数据,此时数据则保存到环境变量$REPLY中
3、指定超时时间
[root@localhost test]# cat read.sh
#!/bin/bash
if read -t 3 -p "Please input your name:" firstName secondName lastName
then
echo "variable is $firstName - $secondName - $lastName"
else
echo -e "\ntimeout\n"
fi
[root@localhost test]# bash read.sh
Please input your name:
timeout
4、从指定文件中读取内容
[root@localhost test]# cat -n test.txt
1 this is test text.
2 this is second line.
3 Hello world
4 C# Main
5 Python
# 使用-u选项
[root@localhost test]# cat readtest.sh
#!/bin/bash
exec 5< ~/test/test.txt
count=0
while read -u 5 var
do
let count=$count+1
echo "Line $count is $var"
done
echo "Total line count is $count"
exec 5<&-
[root@localhost test]# bash readtest.sh
Line 1 is this is test text.
Line 2 is this is second line.
Line 3 is Hello world
Line 4 is C# Main
Line 5 is Python
Total line count is 5
# 使用管道
[root@localhost test]# cat readtest.sh
#!/bin/bash
count=1
cat ~/test/test.txt | while read line
do
echo "Current line $count - $line "
let count=$count+1
done
echo "Total line count is $count"
[root@localhost test]# bash readtest.sh
Current line 1 - this is test text.
Current line 2 - this is second line.
Current line 3 - Hello world
Current line 4 - C# Main
Current line 5 - Python
Total line count is 1
# 使用重定向
[root@localhost test]# cat readtest.sh
#!/bin/bash
count=0
while read line
do
let count=$count+1
echo "Current line $count - $line "
done < ~/test/test.txt
echo "Total line count is $count"
[root@localhost test]# bash readtest.sh
Current line 1 - this is test text.
Current line 2 - this is second line.
Current line 3 - Hello world
Current line 4 - C# Main
Current line 5 - Python
Total line count is 5
本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

Linux 基础教程 45-read命令的更多相关文章
- Linux 基础教程 37-进程命令
pidof 我们知道每个小孩一出生就会一个全国唯一的编号来对其进行标识,用于以后上学,办社保等,就是我们的身份证号.那么在Linux系统中,用来管理运行程序的标识叫做PID,就是大家熟知的进程 ...
- Linux 基础教程 32-解压缩命令
将文件压缩后对提升数据传输效率,降低传输带宽,管理备份数据都有非常重要的功能,因此文件压缩解压技能就成为必备技能.相对于Windows中的文件解压缩工具百花争艳,在Linux中的解压缩工具则要 ...
- Linux基础 - 系统优化及常用命令
目录 Linux基础系统优化及常用命令 Linux基础系统优化 网卡配置文件详解 ifup,ifdown命令 ifconfig命令 ifup,ifdown命令 ip命令 用户管理与文件权限篇 创建普通 ...
- Linux基础系统优化及常用命令
# Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...
- 嵌入式LINUX基础教程 第2版
嵌入式LINUX基础教程 第2版 目录 第1章 入门 11.1 为什么选择Linux 11.2 嵌入式Linux现状 21.3 开源和GPL 21.4 标准及相关组织 31.4.1 Linux标准基 ...
- Linux基础01 学会使用命令帮助
Linux基础01 学会使用命令帮助 概述 在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档:linux系统内置的帮助文档很详细,通常能解决我们的问题, ...
- Linux基础教程 linux中使用find命令搜索文件常用方法记录
find是linux非常强大的搜索命令,通过man find查看find手册,可以发现find的说明一屏接一屏,估计要看完也得花不少时间.兄弟连Linux培训 小编总结了下,整理出find常用的使用方 ...
- Embedded Linux Primer----嵌入式Linux基础教程--2.4节--嵌入式Linux发行版
嵌入式Linux发行版 究竟什么是Linux发行版?在Linux内核引导之后,它期望找到并挂载根文件系统.当一个匹配的根文件系统已经挂载上,启动脚本开始运行大量程序和系统要求的工具.这些程序经常调用其 ...
- Linux基础教程
Linux基础教程之<Linux就该这么学>之学习笔记第一篇... ========================= 一.Basic Linux Commands 基本的Linux ...
- Linux 基础教程 25-命令和文件查找
which 不管是在Windows还是Linux系统中,我们都会偶尔执行一些系统命令,比如Windows常见的cmd.ping.ipconfig等,它们的位置都在%systemdrive%中. ...
随机推荐
- 为eclipse安装python、shell开发环境和SVN插件
http://www.crazyant.net/1185.html 为eclipse安装python.shell开发环境和SVN插件 2013/08/27 by Crazyant 暂无评论 eclip ...
- IPv6 Tunnel Broker+ROS搭建6TO4(IPV6)网络
准备条件:1.公网IPV4的IP2.ROS+IPV6的DHCP,本测试在ROS6.24版本下测试通过3. IPv6 Tunnel Broker:https://www.tunnelbroker.net ...
- 找到一篇关于2.4/5G信道的新介绍
关于部分手机无法搜索到5G wifi信号的解决方法第一次在论坛发基础理论贴,希望能普及关于5G wifi的基础知识. 发此贴的原因是基于本人突然发现:MX3刷了3.4.1系统后,搜索不 ...
- http://www.bootcss.com/p/font-awesome/
集成 将Font Awesome 集成到 Bootstrap 非常容易,还可以被单独使用. 最简单的 Bootstrap + Font Awesome 集成方式 使用这种方式将 Font Awesom ...
- unicat,multicast,broadcast区别
单播.多播和广播单播”(Unicast).“多播”(Multicast)和“广播”(Broadcast)这三个术语都是用来描述网络节点之间通讯方式的术语.那么这些术语究竟是什么意思?区别何在? 1.单 ...
- 可视化库-Matplotlib-饼图与布局(第四天)
1. 画出一个基本的饼图,通过plt.pie() m = 51212 f = 40742 m_perc = m / (m+f) f_perc = f / (m+f) colors = ['navy', ...
- 图像特征与描述子(直方图, 聚类, 边缘检测, 兴趣点/关键点, Harris角点, 斑点(Blob), SIFI, 纹理特征)
1.直方图 用于计算图片特征,表达, 使得数据具有总结性, 颜色直方图对数据空间进行量化,好比10个bin 2. 聚类 类内对象的相关性高 类间对象的相关性差 常用算法:kmeans, EM算法, m ...
- 使用 phpStorm 开发
苦恼蛋疼的曾哥工作室,让人痛不欲生,缓慢的同步速度,另人恼火的插件配置,让人疯狂的卡.简直是让人用了几天之后就不行了. 废话不多说,一款很好的php IDE. 1. phpStorm 下载 here ...
- Redis RDB文件
[Redis RDB文件] 1.RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). RDB 的优点 RDB 是一个非常紧凑(compact)的 ...
- java多线程通信方式之一:wait/notify
java多线程之间的通信方式有多种: 1.wait(),notify(),notifyAll()方法;2.join()方法;3.通过volatile共享内存的方式进行线程通信的;4.interrupt ...