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%中. ...
随机推荐
- 桶排序与快速排序算法结合-python实现
#-*- coding: UTF-8 -*- import numpy as np from QuickSort import QuickSort def BucketSort(a, n): barr ...
- laravel中生成支付宝 二维码 扫码支付
文档教程模拟: http://www.023xs.cn/Article/37/laravel5%E9%9B%86%E6%88%90%E6%94%AF%E4%BB%98%E5%AE%9Dalipay%E ...
- 02 - Unit08:搜索笔记功能、搜索分页、处理插入数据库乱码问题
搜索笔记功能 按键监听事件 $("#search_note").keydown(function(event){ var code=event.keyCode; if(code== ...
- TS流解析 一
一 从TS流开始 数字电视机顶盒接收到的是一段段的码流,我们称之为TS(Transport Stream,传输流),每个TS流都携带一些信息,如Video.Audio以及我们需要学习的PAT.PMT等 ...
- Android Studio使用JDBC远程连接mysql的注意事项(附示例)
JDBC为java程序访问各种类型的关系型数据库提供了统一的接口,用户不必针对不同数据库写出不同的代码,但是使用JDBC必须得下载相应的驱动,比如我这里是要连接mysql,于是就到mysql官网去下载 ...
- [DP题]放苹果
放苹果(DP做法) 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法.输入第一行是测试数据的数目t(0 < ...
- 1021 docker常用命令和Jenkins搭建
docker常用命令 1.镜像管理 docker pull (镜像名) # 默认拉取最新版本的镜像 docker pull (镜像名:版本号) #拉取指定版本的镜像 docker push (镜像名) ...
- Window 端口占用
Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\>netstat -ano 协议 本地地址 外部地址 ...
- win和linux下控制台界面中停顿X秒的方式
win localhost > nul linux
- 「小程序JAVA实战」小程序的个人信息作品,收藏,关注(66)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudegerenxinxizuopinshoucangguanzhu65 ...