阅读bash脚本命令-N1参数 [英] read command in bash script with -N1 argument
问题描述
为什么这个脚本需要-rN1读命令(特别是N1)后?我们为什么要通过只有1人物?
计数器= 0而IFS =读-rN1 _;做
((计数器++))
完成< ≤(查找/等-printf'')回声$文件柜
从帮助读
:
-N nchars只有准确阅读NCHARS字符后返回,除非
EOF遇到或读超时,忽略任何分隔符
块引用>所以这告诉读取读取一个字符后返回。
每当一个字符被读取,
计数
递增。因此,这有效地计算子树文件数的/ etc
。在此设计(这是一个糟糕的设计,看到一个更好的战略 @ hek2mgl 的答案),
找到
吐出的每个文件找到一个空格。只有一个空间(没有换行符等)找到的每个文件。所以,你要读
一次只能读取一个字符,使计数
每次可递增。Why does this script need the -rN1 after the read command (Specifically the N1)? Why would we want to pass only 1 character?
counter=0 while IFS= read -rN1 _; do ((counter++)) done < <(find /etc -printf ' ') echo "$counter files"
解决方案From
help read
:-N nchars return only after reading exactly NCHARS characters, unless EOF is encountered or read times out, ignoring any delimiter
So this tells read to return after reading one character.
Each time one character is read,
counter
is incremented. So this effectively counts the number of files in the subtree/etc
.
In this design (which is a poor design, see @hek2mgl's answer for a better strategy),
find
spits out one space for each file found. Only one space (no newlines, etc.) for each file found. So you wantread
to read only one character at a time, so thatcounter
can be incremented each time.这篇关于阅读bash脚本命令-N1参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!