Shell Read似乎正在从文件(FIFO或常规)中获取新线
我要实现的是将Shell脚本连接到FIFO上,并对某些读取的命令做出反应(使用通常的读取命令)。现在这似乎很容易,但令我惊讶的是,Read命令并没有像我期望的那样做出反应。请参阅以下简单脚本:
#!/bin/bash
while true; do
read ONE_SENTENCE
echo Simon says : ${ONE_SENTENCE}
sleep 1
done
我通过“ ./test.sh< in.pipe”启动此脚本,其中in.pipe
现在为“ mkfifo in.pipe”,如果我用“ echo test1> in”在管道中编写插条。管道“我会得到以下结果:
stc@host:~$ ./test.sh < in.pipe
Simon says : test1
Simon says :
Simon says :
Simon says :
Simon says :
Simon says :
Simon says :
Simon says :
换句话说,阅读不会阻止,它总是会插入读书。 我想念什么?显然,我想阅读以阻止直到新数据
What I want to achieve is to have a shell script hooked up onto a fifo and react to certain commands that would read out (with the usual read command). Now this seems trivial but , by my surprise, the read command does not react as I expected it to. See following simple script :
#!/bin/bash
while true; do
read ONE_SENTENCE
echo Simon says : ${ONE_SENTENCE}
sleep 1
done
I launch this by "./test.sh < in.pipe", where in.pipe is "mkfifo in.pipe"
Now, if I write sthing in the pipe with "echo test1 > in.pipe" I get the following result :
stc@host:~$ ./test.sh < in.pipe
Simon says : test1
Simon says :
Simon says :
Simon says :
Simon says :
Simon says :
Simon says :
Simon says :
In other words, read doesn't block, it find always sthing to read out.
What am I missing ? Obviously, I want read to block until new data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键是仅输出
One_Sentence
在成功的读取后,例如,除了对FIFO的线路的有效读取外,没有产生管道的输出。
一种轻微的变化,可以方便地为您设置FIFO,并在脚本退出中删除它。 (下面避免使用上案例变量)
脚本执行完全相同的事情(除了1/2秒
睡眠
,但它创建了FIFO并将trap
设置为进入读取环之前,将其删除。The key is to only output
ONE_SENTENCE
upon a successful read, e.g.No output from the pipe is produced except on a valid read of a line from the fifo.
A slight variation that conveniently sets the fifo up for you and deletes it on script exit. (upper-case variables are avoided below)
The script does the exact same thing (other than a 1/2 sec
sleep
, but it creates the fifo and sets atrap
to remove it before entering the read-loop.答案很多。与此同时
, 有用
Many thx for the answer. In the meanwhile I've managed to get what I wanted this way
The above code blocks (as expected)... but I still don't understand why the first script didn't block... anyways, one way or the other, it works