[英]Why is read hanging my bash script?
I had a similar problem to this with Python using readlines()
but I'm not sure if it's the same here. 我在使用
readlines()
Python中遇到了与此类似的问题,但是我不确定此处是否相同。
The read
command is hanging my bash script. read
命令挂起了我的bash脚本。
generate_email()
{
# --- Arguments
oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)
refname="$3"
# ... some code ...
}
# ... more code ...
while read oldrev newrev refname
do
generate_email $oldrev $newrev $refname
done
Any ideas on how to fix this? 有想法该怎么解决这个吗?
You're not telling read to read from anything. 您不是在告诉阅读内容。 So it's just waiting for input from
stdin
. 因此,它只是在等待
stdin
。
If you're wanting to read from a file, you need to use read
like so: 如果要读取文件,则需要使用
read
如下所示:
while read -r oldrev newrev refname; do
generate_email "$oldrev" "$newrev" "$refname"
done < /path/to/file
Note the < /path/to/file
. 注意
< /path/to/file
。 That's where you're actually telling read to read from the file. 那就是您实际上告诉read从文件读取的地方。
If you're wanting to read from an input stream, you can use while read
like so: 如果您想从输入流中读取,则可以
while read
使用while read
所示:
grep 'stuffInFile' /path/to/file |
while read -r oldrev newrev refname; do
generate_email "$oldrev" "$newrev" "$refname"
done
I'd say it's not hanging, but just waiting for input. 我会说它没有挂起,只是在等待输入。
Watch out though and make sure that generate_email
does not read from the same input stream. 但是请当心,并确保
generate_email
不会从同一输入流中读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.