Linux命令read

经常我们会使用read命令来遍历读取文件

读取文件

读取文件的方式有很多种,下面介绍使用read命令进行读取,每次遍历文件的每一行,并获取每一行的内容,当文件再没有内容时,read命令会退出并返回非零状态码。

[root@localhost shell]# cat hello.txt 
huangbaokang
hello world
I love you

[root@localhost shell]# cat test.sh 
#!/bin/bash
count=1
cat hello.txt | while read line
do
	echo "Line $count:$line"
	count=$[$count+1]
done
echo "Finished!"
[root@localhost shell]# ./test.sh 
Line 1:huangbaokang
Line 2:hello world
Line 3:I love you
Finished!

给出提示(-p)

[root@localhost shell]# read -p "what's your name?" name
what's your name?huangbaokang
[root@localhost shell]# echo $name
huangbaokang

超时时间(-t)

如下3秒内没有输入,将自动退出。

[root@localhost shell]# read -t 3 -p "goodbye" name
goodbye[root@localhost shell]# 

不带变量,则报输入数据存储在REPLY环境变量

[root@localhost shell]# read
huangbaokang
[root@localhost shell]# echo $REPLY
huangbaokang

隐藏显示(-s)

所有的输入都不是要显示在控制台的,比如输入密码等。

[root@localhost shell]# read -s -p "please input your password:" password
please input your password:[root@localhost shell]# echo $password
huangbaokang
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值