Linux while 和 read 的用法
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 的用法的更多相关文章
- linux mount命令参数及用法详解
linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...
- 【转】linux expr命令参数及用法详解
在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...
- linux dmesg命令参数及用法详解(linux显示开机信息命令)
linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...
- linux sed命令参数及用法详解
linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...
- Linux中yum和apt-get用法及区别
Linux中yum和apt-get用法及区别 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debi ...
- linux下find查找命令用法
Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...
- linux dd命令参数及用法详解---用指定大小的块拷贝一个文件(也可整盘备份)
linux dd命令参数及用法详解---用指定大小的块拷贝一个文件 日期:2010-06-14 点击:3830 来源: 未知 分享至: linux dd命令使用详解 dd 的主要 ...
- linux c语言 select函数用法
linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...
- Linux中find命令的用法汇总
Linux中find命令的用法汇总 https://www.jb51.net/article/108198.htm
- linux 杀死进程kill 等用法
kill -9 $(ps -ef | grep process_name | grep -v grep | awk '{print $2}') 稍微解释一下: awk '{print $2} ...
随机推荐
- 访问需要HTTP Basic Authentication认证的资源的各种开发语言的实现
什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧. 在你访问一个需要H ...
- 【Linux】- Ubuntu安装nginx
安装 执行命令: sudo apt-get install nginx 执行如图: 防火墙设置 查看防火墙状态: sudo ufw status 查看可以穿过防火墙的应用列表: sudo ufw ap ...
- Filezilla 绿色版 禁止升级 能用。
FileZilla还是挺好用的,但是如果钟情于 绿色版的话,肯定首选是 免安装绿色版.但是呢,能找到的所谓的免升级 绿色版,都不能用.只要是打开软件了,就会在你还没有设置更新之前,就已经升级号了.并且 ...
- oracle怎样查询索引的使用情况
查询用户的索引select index_name,table_name,tablespace_name, index_type,uniqueness , status from dba_indexes ...
- xsy1436-括号游戏
题目 递归定义括号序列: 空串是括号序列 (A)是一个括号序列,其中A为括号序列 AB是一个括号序列,其中A,B均为括号序列 定义严格括号序列为形如(A)的括号序列,其中A为括号序列. 给出一个长度为 ...
- BZOJ 1263 整数划分(数学+高精度)
我们不妨考虑可以划分为实数的情况,设划分为x份实数,使得总乘积最大. 易得当每一份都相等时乘积最大.即 ans=(n/x)^x. 现在只需要求出这个函数取得最大值的时候x的取值了. 两边取对数,则有l ...
- 【bzoj2004】[Hnoi2010]Bus 公交线路 状压dp+矩阵乘法
题目描述 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定按下述规则设计 ...
- BZOJ1854:[SCOI2010]连续攻击游戏——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1854 https://www.luogu.org/problemnew/show/P1640 lxh ...
- 洛谷 P2258 子矩阵 解题报告
P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第 2 . 4行和第 ...
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...