Linux 基础教程 45-read命令

摘要:
read命令的基本用法主要用于从标准输入或文件中读取内容,并将信息保存到变量/bin/basecho-n“Pleaseinputyourname:”readnameecho“Hello$name”[root@localhosttest]#巴什里德。shPleasenputourname:JackHelloJack2.指定要从标准输入中读取的显示信息[root@localhosttest]#catread。sh#!/bin/bashifrad-t3-p“请输入您的姓名:”firstNamesecondNamelastNamethencho“变量为$firstName-$secondName-$lastName”elseecho-e“超时”fi[root@localhosttest]#巴什里德。shPleasenputourname:timeout4.从指定文件[root@localhosttest]#目录测试。txt1此列表文本。2是第二条线。3Helloworld4C#Main5Python#使用-u选项[root@localhosttest]#catreadtest.sh#!

基本用法

    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  "
timeout
"
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,或扫描下面的二维码添加关注:
MyQRCode.jpg

免责声明:文章转载自《Linux 基础教程 45-read命令》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【转载】.net题目 —— 加了一些东西重磅出击!!春季小程序活动大作战,看我怎么玩??下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[UE4]虚幻4蓝图使用小技巧

不得不说,虚幻的蓝图系统还是非常方便强大的,大大的提高了开发效率。蓝图是一个很成熟的系统,也就有很多隐藏的小技巧,这些技巧谈不上多高深,却可以使人们在使用蓝图时更加得心应手,更加喜爱这个“可视化编程“。 发现一个更黑科技的技巧…见动图直接把变量拉到节点上,直接生成函数参数啊,类型自动匹配,名字自动匹配!! 按住某个按键,鼠标左键点击蓝图,会在相应地方生成...

动态SQL的使用方法

一般的PL/SQL程序设计中,在DML和事务控制的语句中可以直接使用SQL,但是DDL语句及系统控制语句却不能在PL/SQL中直接使用,要想实现在PL/SQL中使用DDL语句及系统控制语句,可以通过使用动态SQL来实现。   首先我们应该了解什么是动态SQL,在Oracle数据库开发PL/SQL块中我们使用的SQL分为:静态SQL语句和动态SQL语句。所...

记一次linux Microsoft Edge 白板 复原

恢复流程备份/home/make/.config/microsoft-edge-beta/删除/home/make/.config/microsoft-edge-beta/下的文件(非目录)。重新打开。ok 恢复了死机前的数据都在。 重新打开同步 就OK了 恢复过程中使用的其他方法改名 /home/make/.config/microsoft-edge-b...

Velocity模板引擎笔记

模板引擎中判断对象是否为空:  #if(!${jsonObj.data.buyerName} || ${jsonObj.data.buyerName} == '')         <p>采购商名称:$!jsonObj.data.buyerCompanyName</p>  #else         <p>采购商名称:$...

设计模式14---设计模式之命令模式(Command)(行为型)

1.场景模拟 请用软件模拟开机过程 按下启动按钮 然后电源供电 主板开始加电自检 BIOS依次寻找其他设备的BIOS并且让他们初始化自检 开始检测CPU,内存,光盘,硬盘,光驱,串口,并口,软驱即插即用设备 进入系统引导 上面的过程可以抽象为如下: 客户端只是想要发出命令或者请求,不关心请求的真正接受者是谁,也不关心具体如何实现,而且同一个请求的动作可以有...

竞态与线程安全

竞态 对于同样的输入,程序的输出有时候正确而有时候却是错误的。这种一个计算结果的正确性与时间有关的现象就被称为竞态(RaceCondition) 导致竞态的常见原因是多个线程在没有采取任何措施的情况下并发更新、读取同一个共享变量。 竞态往往伴随着数据的脏读问题,即线程读取到一个过时的数据;丢失更新问题,即一个线程丢失数据所做的更新没有体现在后续其他线程对该...