Shell Read似乎正在从文件(FIFO或常规)中获取新线

发布于 2025-01-23 15:42:39 字数 591 浏览 2 评论 0原文

我要实现的是将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2025-01-30 15:42:39

关键是仅输出One_Sentence在成功的读取后,例如,

while :; do
  if read ONE_SENTENCE; then
    [ "$ONE_SENTENCE" = quit ] && break          ## convenient quit ability
    printf "Simon says : %s\n" "$ONE_SENTENCE"   ## output only on good read
  fi
  sleep 1
done

除了对FIFO的线路的有效读取外,没有产生管道的输出。

一种轻微的变化,可以方便地为您设置FIFO,并在脚本退出中删除它。 (下面避免使用上案例变量)

#!/bin/bash

pipe=in.pipe

trap "rm -r $pipe" EXIT

[ -p "$pipe" ] || mkfifo "$pipe"

while :; do
  if read line; then
    [ "$line" = quit ] && break
    printf "Simon says : %s\n" "$line"
  fi
  sleep .5
done < "$pipe"

脚本执行完全相同的事情(除了1/2秒睡眠,但它创建了FIFO并将trap设置为进入读取环之前,将其删除。

The key is to only output ONE_SENTENCE upon a successful read, e.g.

while :; do
  if read ONE_SENTENCE; then
    [ "$ONE_SENTENCE" = quit ] && break          ## convenient quit ability
    printf "Simon says : %s\n" "$ONE_SENTENCE"   ## output only on good read
  fi
  sleep 1
done

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)

#!/bin/bash

pipe=in.pipe

trap "rm -r $pipe" EXIT

[ -p "$pipe" ] || mkfifo "$pipe"

while :; do
  if read line; then
    [ "$line" = quit ] && break
    printf "Simon says : %s\n" "$line"
  fi
  sleep .5
done < "$pipe"

The script does the exact same thing (other than a 1/2 sec sleep, but it creates the fifo and sets a trap to remove it before entering the read-loop.

雨轻弹 2025-01-30 15:42:39

答案很多。与此同时

#!/bin/bash

while true; do
    read ONE_SENTENCE < in.pipe
    echo Simon says : ${ONE_SENTENCE}
    sleep 1
done

​, 有用

Many thx for the answer. In the meanwhile I've managed to get what I wanted this way

#!/bin/bash

while true; do
    read ONE_SENTENCE < in.pipe
    echo Simon says : ${ONE_SENTENCE}
    sleep 1
done

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文