Linux上read命令的使用

一:read倾听是一种美德
 
    1.倾听键盘的输入并保存到变量中
      
      例如:#! /bin/bash
           echo  "please input your name"
           read  name
           echo  "welcome  $name !"
           exit 0  
      或是read自带的显示提示语
      例如:
          #! /bin/bash
          read  -p  "please input two  args" name  place 
  echo  "welcome  $name to $place"
          exit 0
    
    2.如果不输入变量,其值保存在REPLY变量中
       例如:
           #! /bin/bash
           read  -p  "please input two  args"  
   echo  "the content of you  just input is $REPLY"
           exit 0
    
    3.read加时间限制
 
           #! /bin/bash
           # 5秒内不输入则
           if read -t 5 -p "please input two  args"  
   then
              echo  "the content of you  just input is $REPLY"
           else
              echo  "sorry you are too late"
           exit 0
    
    4.隐蔽输入内容
           #! /bin/bash
           read  -s  -p  "please input your password"  password
           echo  "here,your  password is $password"
     
    5.使用 -u选项读取文件
           #! /bin/bash
           exec  3<~/文档/test.txt     # ~/文档/test.txt代表的是读取的文件目录
           count=0
           while  read  -u 3 -r var  #这里使用-r预防读入每行时未正常换行
           do
             let  count=$count+1  #let是执行计算的命令
             echo  "LIne  $count:$var"
           done
           echo  "finished!"
           echo  "Line num is  $count"
           exec  3<&-   #关闭文件描述符
原文地址:https://www.cnblogs.com/haibiscuit/p/10447713.html