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$0scripts.."

ls -l /proc/$mypid/fd

# Close fd # 3 and # 4

exec 3

exec 4>&-

while read 的用法

从标准输入读取

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

从管道读取

$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:$0filename"; exit 1; }

# make sure file exist else die

[ ! -f $FILE] && { echo "Error - File$FILEdoes not exists." ; exit 2; }

# make sure file readonly else die

[ ! -r $FILE] && { echo "Error - Can not read$FILEfile."; exit 3; }

IFS=$(echo -en "\n\b")

exec 3

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

原文:https://www.cnblogs.com/tiantiandas/p/shell_command_read.html

linux command read the content,Linux while 和 read 的用法相关推荐

  1. Linux Command pushd

    Linux Command pushd 文章目录 Linux Command pushd 1. 简介 2. 格式 3. 参数 4. 实例 1. 简介 pushed命令用于将目录加入堆栈中,加入记录到目 ...

  2. Linux Command egrep

    Linux Command egrep 文章目录 Linux Command egrep 1. 特点 2. 举例 1. 特点 基本曾则:优点(兼容性强,缺点(繁琐)) 拓展:优点(简单),缺点:(兼容 ...

  3. linux command find

    Linux command find [Purpose]        Learning linux command nmon   [Eevironment]        Ubuntu 16.04 ...

  4. 解决: -bash: docker-compose: command not found、linux 安装 docker-compose

    https://blog.csdn.net/jiangyu1013/article/details/84570872 https://blog.csdn.net/guoshaoliang789/art ...

  5. Linux Command sar 网卡流量

    Linux Command sar 网卡流量 tags: 网络, 监控 文章目录 Linux Command sar 网卡流量 1. 简介 2. 查看内存各个指标的变化情况 1. 简介 如何实时查看网 ...

  6. Linux Command who、whois、whoami

    Linux Command who.whois.whoami tags: 用户管理 1. whois 显示指定用户信息 $ whois root #显示指定用户信息 $ whois ywnz.com ...

  7. Linux Command tc 模拟网络延迟和丢包

    Linux Command tc 模拟网络延迟和丢包 文章目录 Linux Command tc 模拟网络延迟和丢包 1. 介绍 2. 规则 2.1 流量控制方式 2.2 流量控制处理对象 3. 操作 ...

  8. Linux Command iperf3网络测速工具

    Linux Command iperf3网络测速工具 文章目录 Linux Command iperf3网络测速工具 1. 简介 2. 安装 3. 功能 4. 参数 5. 示例 5.1 测试TCP吞吐 ...

  9. OS + linux command / Linux Command / Linux command / linux Command

    写下你职业生涯中最难以忘怀的误操作.. http://www.dangkai.com/ArticlePage/Article59549.htm http://bbs.chinaunix.net/thr ...

最新文章

  1. 为AD用户启用或禁用OCS 2007 R2帐户
  2. 移动端前端开发注意点(未完待续)
  3. 【机器学习基础】数学推导+纯Python实现机器学习算法15:GBDT
  4. 03 - const static extern
  5. 微信开放平台 公众号第三方平台开发 教程五 代公众号发起网页授权源码
  6. 公交站台被圈进养鸭场
  7. 第一篇博客《import tensorflow的问题解决》
  8. 在C#应用程序中嵌入暴风影音播放器
  9. 「大数据干货」基于Hadoop的大数据平台实施——整体架构设计
  10. matlab 计算相关系数,MATLAB如何使用corrcoef函数计算样本数据的相关系数
  11. Unity塔防游戏学习(六)
  12. 【rmzt:进击的巨人三笠帅气主题】
  13. 佳博80系列小票打印机 gp 80180安装驱动,显示打印错误
  14. (翻译)稀缺模式(Scarcity)
  15. 编写高质量的代码——从命名入手
  16. 科目二 倒车入库 流程记录 LTS
  17. mysql decimal类型转date类型
  18. 听框架师——李云谈如何成为一个技术“牛人”
  19. ANSYS多孔结构 多孔材料 孔隙结构建模教程
  20. mes系统故障_MES系统目前存在哪些问题?

热门文章

  1. Java判断一个数是不是快乐数
  2. [转] Mou 一个Markdown工具 语法规则文档(最后)
  3. Android教程 -07 Activity的任务栈和启动模式
  4. 随手小记 才知道[阁楼藏尸|未来闪影]
  5. 重装操作系统时遇到的一些问题的解决方法总结
  6. ps画布扩展颜色灰色不可用的原因和解决方法
  7. 图解抖音推荐算法(布局抖音短视频的可以参考)
  8. 【白皮书分享】2020中国美颜消费趋势白皮书.pdf(附下载链接)
  9. 数据挖掘之CTR预估(FM算法)
  10. 上百篇论文概览『负采样方法』的前世今生