打印fifo内容并退出

发布于 2024-07-19 05:04:57 字数 298 浏览 2 评论 0原文

我需要将 fifo(命名管道)的内容打印到标准输出。
我可以使用命令:

cat fifo

问题是 cat 没有返回。 它保持运行状态,等待来自 fifo 的更多内容。 但我知道一段时间内不会有更多内容,所以我只想打印可用的内容。

是否有一个命令只打印可用内容并退出?

编辑:
在 fifo 的一端有一个进程时不时地写入不同命令的输出。 该进程永久运行,因此不会出现 EOF。

I need to print the content of a fifo (named pipe) to standard output.
I could use the command:

cat fifo

The problem is that cat doesn't return. It stays running, waiting for more content coming from the fifo. But I know there wont be any more content coming for a while so I just want to print what's available.

Is there a command that just print the available content and exit??

EDIT:
In one end of the fifo there is a process writing every now and then the output of different commands. That process is permanently running so there wont be an EOF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鸠书 2024-07-26 05:04:57

当您无法发送 EOF 时,您可以使用“非阻塞猫”。 我已经包含了我发现的(经过测试的)C 版本 这里(当然,归功于那里的原作者)。 神奇之处在于 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK)。

这个非阻塞猫的第一个参数是您在再次退出之前要等待的秒数。

#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <string.h>


void read_loop(int fFile, double wWait)
{
    if (fFile < 0) return;

    double max_time = wWait, total_time = 0;
    struct timespec cycle_time = { 0, 50 * 1000 * 1000 };
    double add_time = (double) cycle_time.tv_sec + (double) cycle_time.tv_nsec / 1000000000.;

    char next_line[1024];

    FILE *input_file = fdopen(fFile, "r");

    while (total_time < max_time)
    {
    while (fgets(next_line, 1024, input_file))
     {
    write(STDOUT_FILENO, next_line, strlen(next_line));
    total_time = 0;
     }

    nanosleep(&cycle_time, NULL);
    total_time += add_time;
    }

    fclose(input_file);
}


int main(int argc, char *argv[])
{
    if (argc < 2)
    {
    fprintf(stderr, "%s [max time] (files...)\n", argv[0]);
    return 1;
    }

    int max_wait = strtoul(argv[1],0, 10);

    if (argc == 2)
    {
    fprintf(stderr, "%s: using standard input\n", argv[0]);
    fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
    read_loop(STDIN_FILENO, max_wait);
    return 0;
    }

    int current = 2;

    while (current < argc)
    {
    fprintf(stderr, "%s: switch to file '%s'\n", argv[0], argv[current]);
    int next_file = open(argv[current++], O_RDONLY | O_NONBLOCK);
    read_loop(next_file, max_wait);
    close(next_file);
    }

    return 0;
}

When you can't send an EOF, you could use a 'non-blocking cat'. I've included a (tested) C version i found here (credit goes to the original author over there of course). The magic is in fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK).

The first argument to this non-blocking cat is the number of seconds you want to wait before exiting again.

#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <string.h>


void read_loop(int fFile, double wWait)
{
    if (fFile < 0) return;

    double max_time = wWait, total_time = 0;
    struct timespec cycle_time = { 0, 50 * 1000 * 1000 };
    double add_time = (double) cycle_time.tv_sec + (double) cycle_time.tv_nsec / 1000000000.;

    char next_line[1024];

    FILE *input_file = fdopen(fFile, "r");

    while (total_time < max_time)
    {
    while (fgets(next_line, 1024, input_file))
     {
    write(STDOUT_FILENO, next_line, strlen(next_line));
    total_time = 0;
     }

    nanosleep(&cycle_time, NULL);
    total_time += add_time;
    }

    fclose(input_file);
}


int main(int argc, char *argv[])
{
    if (argc < 2)
    {
    fprintf(stderr, "%s [max time] (files...)\n", argv[0]);
    return 1;
    }

    int max_wait = strtoul(argv[1],0, 10);

    if (argc == 2)
    {
    fprintf(stderr, "%s: using standard input\n", argv[0]);
    fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
    read_loop(STDIN_FILENO, max_wait);
    return 0;
    }

    int current = 2;

    while (current < argc)
    {
    fprintf(stderr, "%s: switch to file '%s'\n", argv[0], argv[current]);
    int next_file = open(argv[current++], O_RDONLY | O_NONBLOCK);
    read_loop(next_file, max_wait);
    close(next_file);
    }

    return 0;
}
短暂陪伴 2024-07-26 05:04:57

您应该关闭 FIFO 的另一端。 这应该向 cat 进程发送一个 EOF。

You should close the other end of the FIFO. That should send an EOF to the cat process.

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