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} ...
随机推荐
- web前端之路 - 开篇
一 web发展历程 了解事物的历史有助于我们渐进式的从发展的思路清楚了解事物的来龙去脉. 这里有一篇网文写得比较清晰和完整:https://www.tianmaying.com/tutorial/we ...
- ZOJ 1913 J-Eucild's Game
https://vjudge.net/contest/67836#problem/J Two players, Stan and Ollie, play, starting with two natu ...
- Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"的解决办法
最近更新了一下Android Studio(下文简写成AS),然后打开工程发现出现Error:Unable to tunnel through proxy. Proxy returns "H ...
- 【Python】python-内置常量
引言 Python的内置常量不多,只有6个,分别是True.False.None.NotImplemented.Ellipsis.__debug__ 一.True 1.True是bool类型用来表示的 ...
- c/c++中的关键字(static、const、inline、friend)
static:1.a.c语言中static修饰的局部变量在编译时赋初始值,只赋初始值一次,在函数运行时已有初值,每次调用函数时不用重新赋值,指示保留上次 函 数调用结束时的值. 如果定义局部变量不赋初 ...
- hdu 2768 Cat vs. Dog (二分匹配)
Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)
这题做得比较复杂..应该有更好的做法 题目大意: 有一个括号序列,可以对其进行两种操作: · 向里面加一个括号,可以在开头,在结尾,在两个括号之间加. · 对当前括号序列进 ...
- Linux相关——关于gdb的checkpoint & breakpoints指令
1,checkpoint ,,,这个指令简直,,,相见恨晚啊,居然现在才发现,.. 好吧来介绍一下这个指令:checkpoint(检查点) 我们调试程序,常常会出现好不容易发现了错误,却已经跑完那个地 ...
- POJ2142:The Balance——题解
http://poj.org/problem?id=2142 题目大意:有一天平和两种数量无限的砝码(重为a和b),天平左右都可以放砝码,称质量为c的物品,要求:放置的砝码数量尽量少:当砝码数量相同时 ...
- BZOJ4651 & 洛谷1173 & UOJ220:[NOI2016]网格——题解(附debug数据)
https://www.lydsy.com/JudgeOnline/problem.php?id=4651 https://www.luogu.org/problemnew/show/P1173#su ...