基本用法

    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命令的更多相关文章

  1. Linux 基础教程 37-进程命令

    pidof     我们知道每个小孩一出生就会一个全国唯一的编号来对其进行标识,用于以后上学,办社保等,就是我们的身份证号.那么在Linux系统中,用来管理运行程序的标识叫做PID,就是大家熟知的进程 ...

  2. Linux 基础教程 32-解压缩命令

        将文件压缩后对提升数据传输效率,降低传输带宽,管理备份数据都有非常重要的功能,因此文件压缩解压技能就成为必备技能.相对于Windows中的文件解压缩工具百花争艳,在Linux中的解压缩工具则要 ...

  3. Linux基础 - 系统优化及常用命令

    目录 Linux基础系统优化及常用命令 Linux基础系统优化 网卡配置文件详解 ifup,ifdown命令 ifconfig命令 ifup,ifdown命令 ip命令 用户管理与文件权限篇 创建普通 ...

  4. Linux基础系统优化及常用命令

    # Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...

  5. 嵌入式LINUX基础教程 第2版

    嵌入式LINUX基础教程  第2版 目录 第1章 入门 11.1 为什么选择Linux 11.2 嵌入式Linux现状 21.3 开源和GPL 21.4 标准及相关组织 31.4.1 Linux标准基 ...

  6. Linux基础01 学会使用命令帮助

    Linux基础01 学会使用命令帮助 概述 在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档:linux系统内置的帮助文档很详细,通常能解决我们的问题, ...

  7. Linux基础教程 linux中使用find命令搜索文件常用方法记录

    find是linux非常强大的搜索命令,通过man find查看find手册,可以发现find的说明一屏接一屏,估计要看完也得花不少时间.兄弟连Linux培训 小编总结了下,整理出find常用的使用方 ...

  8. Embedded Linux Primer----嵌入式Linux基础教程--2.4节--嵌入式Linux发行版

    嵌入式Linux发行版 究竟什么是Linux发行版?在Linux内核引导之后,它期望找到并挂载根文件系统.当一个匹配的根文件系统已经挂载上,启动脚本开始运行大量程序和系统要求的工具.这些程序经常调用其 ...

  9. Linux基础教程

    Linux基础教程之<Linux就该这么学>之学习笔记第一篇... ========================= 一.Basic Linux Commands    基本的Linux ...

  10. Linux 基础教程 25-命令和文件查找

    which     不管是在Windows还是Linux系统中,我们都会偶尔执行一些系统命令,比如Windows常见的cmd.ping.ipconfig等,它们的位置都在%systemdrive%中. ...

随机推荐

  1. Haroopad安装与配置: Linux系统下最好用的Markdown编辑器

    1. Haroopad概述 Haroopad is a markdown enabled document processor for creating web-friendly documents. ...

  2. emacs配置emacs-clang-complete-async

    debian下需要安装apt安装下clang和llvm sudo apt-get install llvm-dev sudo apt-get install libclang-dev 网上抄来一个大神 ...

  3. jsp中9个隐含对象

    在JSP中一共有9个隐含对象,这个9个对象我可以在JSP中直接使用.因为在service方法已经对这个九个隐含对象进行声明及赋值,所以可以在JSP中直接使用. - pageContext 类型:Pag ...

  4. mac下执行 appium-doctor 出现 “Could not detect Mac OS X Version from sw_vers output: '10.12 '” 解决方法

      You can config file by this command: grep -rl "Could not detect Mac OS X Version from sw_vers ...

  5. Bootstrap-CSS

    ylbtech-Bootstrap-Plugin: 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1.   2.   11.返回顶部   作者:yl ...

  6. [转]命令行在IIS添加虚拟目录

    来自:http://www.jb51.net/softjc/29702.htmlMkwebdir -c LocalHost -w "Default Web Site" –v Com ...

  7. python渗透测试工具包

    网络 Scapy, Scapy3k: 发送,嗅探,分析和伪造网络数据包.可用作交互式包处理程序或单独作为一个库.pypcap, Pcapy, pylibpcap: 几个不同 libpcap 捆绑的py ...

  8. ExtJS组件扩展

    1.extends 2.initComponent 3.constracot: 4.onRender:重新写这个方法 ========================================= ...

  9. django-model的元类Meta

    Meta类存在model类里面 模型元选项 文档有更多Meta类的配置属性: English:https://docs.djangoproject.com/en/1.11/ref/models/opt ...

  10. generator+列表生成式输出杨辉三角形

    杨辉三角形: 1                  1    1              1    2    1         1     3    3     1    1      4    ...