美文网首页Linux基础学习教程
Linux 基础教程 45-read命令

Linux 基础教程 45-read命令

作者: Surpassme | 来源:发表于2018-09-02 23:46 被阅读1次

基本用法

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

MyQRCode.jpg

相关文章

  • Linux 基础教程 45-read命令

    基本用法     read命令主要用于从标准输入读取内容或从文件中读取内容,并把信息保存到变量中。其常用用法如下所...

  • Linux基础教程

    Linux基础教程 一、常用命令使用 1.1 常用命令使用 1.1.1 登录和退出Linux系统 1. 启动和登陆...

  • 3.1Linux基础及常用命令

    本节将分享Linux的基础教程及常用命令。 什么是Linux? Linux是三种主流的操作系统之一,其他两种是我们...

  • 写作自检清单

    博客搭建系列Markdown教程Git教程系列基础教学系列linux 命令教学系列配置系列太基础教程系列广告系列诸...

  • Shell学习

    Linux Shell基础教程 (一) (二) Linux Shell简明教程(推荐) (一) (二) Linux...

  • Python入门教程完整版(懂中文就能学会)(600集)

    目录大纲: 本套教程15天 学前环境搭建 1-3天内容为Linux基础命令 4-13天内容为Python基础教程 ...

  • 面试题 2021-11-01~2021-11-12

    常用的Linux命令 Linux命令 - Linux安全网 - Linux操作系统_Linux 命令_Linux教...

  • linux 命令

    vi 编辑器命令 linux重启命令 linux ssh命令 linux scp命令

  • Web Jenkins 自动构建打包

    Linux命令参考链接 Linux命令大全Linux命令大全Linux常用命令大全 本文可能到的命令: Jenki...

  • Linux

    linux awk命令详解、linux awk命令Linux常用操作指令Linux netstat命令详解 awk...

网友评论

    本文标题:Linux 基础教程 45-read命令

    本文链接:https://www.haomeiwen.com/subject/qfvvwftx.html