Reference: [ linux man doc ] [ CSDN roler_ ] [ Reads from the file descriptor]

read 命令说明

SYNTAX : read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

**OPTIONS ** :

-r : 忽视转义符

-s : 静默模式. 对于 [ 输入密码 ] 的需求.

-a ARRAY_NAME : 将输入的字符存入数组.

-d : 用 -d 参数值的第一个字符定义结束符. 默认为换行符.

-n NUMBER: 输入 -n 参数值定义的字符个数时, 自动结束. 当使用 Enter 时, 结束交互.

-N NUMBER : 输入 -N 指定参数的字符个数时, 自动结束. 当使用 Enter 的时候, 不结束交互.

-p CONTENT : 交互时的提示信息

-t NUMBER: 超时时间, 单位 : 秒 (s)

-u FD: 从文件描述符中获取输入

EXAMPLES

read 如果不指定变量名称, 则输入的值默认赋给: REPLY

$read
First Line
$echo $REPLY
First Line

-s : 静默模式

$read -s -p "Input Your Password:" PASSWD
Input Your Password: $echo $PASSWD
testpass

-a : 读入数据存入数组

$read -a number_array -p "Input number sequence:"
Input number sequence:10 20 50 100
$
$for n in ${number_array[@]}; do echo $n;done
10
20
50
100

-u : 从文件描述符读入数据

#!/bin/bash
# Let us assign the file descriptor to file for input fd # 3 is Input file
exec 3< /etc/resolv.conf # Let us assign the file descriptor to file for output fd # 3 is Input file
exec 4> /tmp/output.txt # Use read command to read first line of the file
read -u 3 a b echo "*** My pid is $$"
mypid=$$ echo "*** Currently open files by $0 scripts.." ls -l /proc/$mypid/fd # Close fd # 3 and # 4
exec 3<&-
exec 4>&-

while read 的用法

从标准输入读取

while read line
do
echo $line
done</etc/resolv.conf # result
; generated by /usr/sbin/dhclient-script
search lvrpf0txgxle3htzsh2w1rj15g.hx.internal.cloudapp.net
nameserver 168.63.129.16

从管道读取

$cat /etc/resolv.conf | while read line;do echo $line;done

# result
; generated by /usr/sbin/dhclient-script
search lvrpf0txgxle3htzsh2w1rj15g.hx.internal.cloudapp.net
nameserver 168.63.129.16

从文件描述符读取

#!/bin/bash
# Shell script utility to read a file line line.
FILE="$1" # make sure filename supplied at a shell prompt else die
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; } # make sure file exist else die
[ ! -f $FILE ] && { echo "Error - File $FILE does not exists." ; exit 2; } # make sure file readonly else die
[ ! -r $FILE ] && { echo "Error - Can not read $FILE file."; exit 3; } IFS=$(echo -en "\n\b") exec 3<$FILE
while read -u 3 -r line
do
echo $line
done # Close fd # 3
exec 3<&- # exit with 0 success status
exit 0

定义分隔符

利用环境变量: IFS 可以定义分隔符

while IFS=: read user _ _ _ _ home shell
do
echo "user: $user. home: $home. shell: $shell"
done</etc/passwd

Linux while 和 read 的用法的更多相关文章

  1. linux mount命令参数及用法详解

    linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...

  2. 【转】linux expr命令参数及用法详解

    在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...

  3. linux dmesg命令参数及用法详解(linux显示开机信息命令)

    linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...

  4. linux sed命令参数及用法详解

    linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...

  5. Linux中yum和apt-get用法及区别

    Linux中yum和apt-get用法及区别   一般来说著名的linux系统基本上分两大类:   1.RedHat系列:Redhat.Centos.Fedora等   2.Debian系列:Debi ...

  6. linux下find查找命令用法

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  7. linux dd命令参数及用法详解---用指定大小的块拷贝一个文件(也可整盘备份)

    linux dd命令参数及用法详解---用指定大小的块拷贝一个文件 日期:2010-06-14 点击:3830 来源: 未知 分享至:            linux dd命令使用详解 dd 的主要 ...

  8. linux c语言 select函数用法

    linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...

  9. Linux中find命令的用法汇总

    Linux中find命令的用法汇总 https://www.jb51.net/article/108198.htm

  10. linux 杀死进程kill 等用法

    kill -9 $(ps -ef | grep process_name | grep -v grep | awk  '{print $2}')   稍微解释一下:  awk  '{print $2} ...

随机推荐

  1. com技术学习

    百度百科概念 COM是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术.在COM构架下,人们可以开发出各种各样的功能专一的组件,然后将它们按照需要组合起来,构成复杂的应 ...

  2. iOS关于setContentOffset的一些细节问题

    在UIScrollView,setContentOffset方法的功能是跳转到你指定内容的坐标, setContentOffset有两种方法:setContentOffset:和setContentO ...

  3. python 爬虫 糗百成人

    import urllib from time import sleep import requests from lxml import etree try: def all_links(url,p ...

  4. (转)Linux常用性能检测命令

    一.uptime     Uptime命令的显示结果包括服务器已经运行了多长时间,有多少登陆用户和对服务器性能的总体评估(load average).load average值分别记录了上个1分钟,5 ...

  5. Apache与Tomcat负载均衡

    Apache HTTP Server 与 Tomcat 的三种连接方式JK,http_proxy,ajp_proxy.下面逐个介绍一下(本篇介绍的示例都是基于前面介绍的已经搭建好的Tomcat集群,都 ...

  6. Bootstrap 轮播图的使用和理解

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  7. 【bzoj4278】[ONTAK2015]Tasowanie 贪心+后缀数组

    题目描述 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. 输入 第一行包含一个正整数n(1<=n<=200000),表示A串的长度. 第二行包含 ...

  8. BZOJ4868 Shoi2017期末考试(三分+贪心)

    容易想到枚举最晚发布成绩的课哪天发布,这样与ti和C有关的贡献固定.每门课要么贡献一些调节次数,要么需要一些调节次数,剩下的算贡献也非常显然.这样就能做到平方级别了. 然后大胆猜想这是一个凸函数三分就 ...

  9. 协程-Greenlet

    协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈. 线程切换的时候会保存到CPU里面. 因此: 协程能保留上一次调用时的 ...

  10. 转:Scipy入门

    Scipy入门 转:http://notes.yeshiwei.com/scipy/getting_started.html 本章节主要内容来自 Getting Started .翻译的其中一部分,并 ...