read命令用于从标准输入中读取输入单行,并将读取的单行根据IFS变量分裂成多个字段,并将分割后的字段分别赋值给指定的变量列表var_name。第一个字段分配给第一个变量var_name1,第二个字段分配给第二个变量var_name2,依次到结束。如果指定的变量名少于字段数量,则多出的字段数量也同样分配给最后一个var_name,如果指定的变量命令多于字段数量,则多出的变量赋值为空。

如果没有指定任何var_name,则分割后的所有字段都存储在特定变量REPLY中

常用参数
-p:给出提示符。默认不支持"\n"换行
-s:静默模式。输入的内容不会回显在屏幕上
-t:给出超时时间,在达到超时时间时,read退出并返回错误
-n:限制读取N个字符就自动结束读取,如果没有读满N个字符就按下回车或遇到换行符,则也会结束读取
-N:严格要求读满N个字符才自动结束读取,即使中途按下了回车或遇到了换行符也不结束。其中换行符或回车算一个字符
-a:将分裂后的字段依次存储到指定的数组中,存储的起始位置从数组的index=0开始
-p:给出提示符

简短示例

#!/bin/bash
read -p "print a number: " num
if [ $num -eq 5 ]; then
echo "input right" && echo $num
else
echo "input error"
fi
exit
#执行
[root@localhost testsh]#./read.sh
print a number: 5
input right
如果没有指定变量,read会把传入的值传给$REPLY,只要调用$REPLY就可以引用
#!/bin/bash

read -p "print a number: "
if [ $REPLY -eq 5 ]; then
echo "input right" && echo $REPLY
else
echo "input error"
fi
exit
-t:给出超时时间,在达到超时时间时,read退出并返回错误
#!/bin/bash
##
if read -t 5 -p "input a number: " num
then
echo "$num inputed"
else
echo "timeout"
fi
exit
#执行
[root@localhost testsh]#./timeout.sh
input a number: timeout
-n:限制读取N个字符就自动结束读取,如果没有读满N个字符就按下回车或遇到换行符,则也会结束读取
[root@localhost testsh]#read -n 5
12345[root@localhost testsh]#
[root@localhost testsh]#read -n 5
123
-a:将分裂后的字段依次存储到指定的数组中,存储的起始位置从数组的index=0开始
[root@localhost testsh]#read -a value
1 2 3 4
[root@localhost testsh]#echo ${value[0]}
1
[root@localhost testsh]#echo ${value[1]}
2
[root@localhost testsh]#echo ${value[2]}
3
[root@localhost testsh]#echo ${value[3]}
4
read读取文件内容
#!/bin/bash
##
#read file ip.txt
cat /mnt/ip.txt | while read IP
do
echo "the ip : $IP"
done
echo "finsh"
exit
执行读取ip.txt内容结果
the ip : 192.168.11.10
the ip : 192.168.11.11
the ip : 192.168.11.12
the ip : 192.168.11.13
the ip : 192.168.11.14
the ip : 192.168.11.15
the ip : 192.168.11.16
the ip : 192.168.11.17
the ip : 192.168.11.18
the ip : 192.168.11.19
finsh
或者使用for循环读取
for IP in `cat /mnt/ip.txt`
do
echo $IP
done
read指令还可以将接收的值作为case语句判断的条件值,使用如下
#!/bin/bash
read -t 30 -p "Do you want exit script[Y/N]?" char
case "$char" in
"Y" | "y")
echo "bye"
;;
"N" | "n")
echo "stay"
;;
*)
echo "output error,please input N,n/Y.y"
;;
esac

执行

[root@localhost testsh]#./bak.sh 
Do you want exit script[Y/N]?n
stay

read指令使用方法的更多相关文章

  1. controller 和 指令 通讯方法

    在 angular 中我们经常会使用多个 controller 和 指令 他们拥有各自的 $scope , 这就产生了跨$scope调用的问题. 有几种常见的方法来可以使用. 方法一 : 指令 req ...

  2. Xshell同时向多个会话发送指令的方法

    我们平时使用XSHELL.SecureCRT.putty等ssh连接工具连接到远程主机,每次输入指令都是在单一会话窗口,如果有很多台会话,需要同时输入同样的指令,我们就不用一一输入,浪费时间和精力.可 ...

  3. SDI011 读卡器自动发送00A4选择指令 解决方法

    如标题,SDI读卡器会自动发送 004A的应用选择指令 解决方法: 是Certificate Propagation 服务 弄的, 关闭就好了

  4. nginx root、alias、location指令使用方法

    一.nginx root指令 1. Nginx配置 相关配置如下图: 通过配置root目录到"/wwwroot/html/"位置 在用虚拟主机方法,主机名称是test,需要大家配置 ...

  5. Vue自定义指令使用方法详解 和 使用场景

    Vue自定义指令的使用,具体内容如下 1.自定义指令的语法 Vue自定义指令语法如下: Vue.directive(id, definition) 传入的两个参数,id是指指令ID,definitio ...

  6. ARM汇编指令调试方法

    学习ARM汇编时,少不了对ARM汇编指令的调试.作为支持多语言的调试器,gdb自然是较好的选择.调试器工作时,一般通过修改代码段的内容构造trap软中断指令,实现程序的暂停和程序执行状态的监控.为了在 ...

  7. V-MODEL指令实现方法

    V-MODEL 是VUE 的一个指令,在input 控件上使用时,可以实现双向绑定. 通过看文档,发现他不过是一个语法糖. 实际是通过下面的代码来实现的:   <%@ page language ...

  8. Dockerfile的书写规则及指令使用方法

    Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令.Docker程序将读取Dockerfile,根据指令生成定制的ima ...

  9. Vue学习五:v-for指令使用方法

    本文为博主原创,未经允许不得转载: <!DOCTYPE html> <html lang="zh"> <head> <meta http- ...

  10. Vue学习四:v-if及v-show指令使用方法

    本文为博主原创,未经允许不得转载: <!DOCTYPE html> <html lang="zh"> <head> <meta http- ...

随机推荐

  1. 配置文件中取值: spring配置文件中util:properties和context:property-placeholder

    转载大神 https://blog.csdn.net/n447194252/article/details/77498916 util:properties和context:property-plac ...

  2. 在HEXO主题中添加数学公式支持

    在markdown中书写数学符号的方式参考Latex常用数学符号 Mathjax 安装 npm uninstall hexo-renderer-marked --save npm install he ...

  3. ProtocBuffer安装

    学习protocolhttp://www.jianshu.com/p/fa126a8535a0 mac安装protocbuff: 下边总结一下安装流程: 查看官方文档源码在 https://githu ...

  4. adb 调系统时间

    1.修改前提 获取系统root权限,然后adb shell进入shell界面 adb shell su 2.时区设置 cat /data/property/persist.sys.timezone / ...

  5. how to keep impact-crusher in good condition

    how to keep impact-crusher in good condition Why we have to maintenance impact crusher? As we talked ...

  6. Linux Shell 中数组的语法及应用

    #!/bin/sh## 数组的声明与初始化方法# 先声明后赋值:declare -a arrayarray=(one two three) # 声明并初始化:array_1=(1 2 3 four) ...

  7. jquery.validate+jquery.form表单验证提交

    1.通过jquery.validate的submitHandler选项,即当表单通过验证时运行回调函数.在这个回调函数中通过jquery.form来提交表单: <script type=&quo ...

  8. DNS跳转

    switch (window.location.hostname) { case "www.zcom.gov.cn" ://确定域名为 www.zcom.gov.cn //wind ...

  9. Lambda动态排序通用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  10. 【练习笔记】spring 配置Schedule

    spring项目一些简单的定时任务可以通过@Schedule注解来实现,具体配置如下 在applicationContext.xml文件中增加配置 1.引入task约束 xmlns:task=&quo ...