先来回顾一下管道,通过管道符号,我们可以将命令的输出作为标准输入到其他的命令:

    1. $ cat txt | grep a | less -S

    mkfifo 可以创建一个命名管道,我们可以将一个终端中运行的结果通过这个命名管道,在另外一个终端访问。

    首先创建一个命名管道:

    1. $ mkfifo pipe1
    2. $ ls
    3. pipe1
    4. prw-rw-r-- 1 yzpeng yzpeng 0 1 23 15:11 pipe1

    可以看到文件类型都不一样, p

    接着就可以将命令的结果重定向到该管道文件中:
    image.png
    可以看到,shell 正在等待我们的使用。

    这时候在其他终端中进入文件目录访问该命名管道:

    1. $ cat pipe1
    2. 0.bin
    3. 1.pipeline
    4. 2.data
    5. 3.biosoft
    6. archive
    7. jimmy
    8. mess
    9. miniconda3

    而另外一边也同时结束了:
    image.png

    ps:乍一看,好像也没有什么用呀~

    或许以后会有大帮助吧。

    看看它的帮助文档:

    1. $ man mkfifo | cat
    2. MKFIFO(1) User Commands MKFIFO(1)
    3. NAME
    4. mkfifo - make FIFOs (named pipes)
    5. SYNOPSIS
    6. mkfifo [OPTION]... NAME...
    7. DESCRIPTION
    8. Create named pipes (FIFOs) with the given NAMEs.
    9. Mandatory arguments to long options are mandatory for short options
    10. too.
    11. -m, --mode=MODE
    12. set file permission bits to MODE, not a=rw - umask
    13. -Z set the SELinux security context to default type
    14. --context[=CTX]
    15. like -Z, or if CTX is specified then set the SELinux or SMACK
    16. security context to CTX
    17. --help display this help and exit
    18. --version
    19. output version information and exit
    20. AUTHOR
    21. Written by David MacKenzie.
    22. REPORTING BUGS
    23. GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
    24. Report mkfifo translation bugs to <https://translationpro
    25. ject.org/team/>
    26. COPYRIGHT
    27. Copyright © 2018 Free Software Foundation, Inc. License GPLv3+: GNU
    28. GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
    29. This is free software: you are free to change and redistribute it.
    30. There is NO WARRANTY, to the extent permitted by law.
    31. SEE ALSO
    32. mkfifo(3)
    33. Full documentation at: <https://www.gnu.org/software/coreutils/mkfifo>
    34. or available locally via: info '(coreutils) mkfifo invocation'
    35. GNU coreutils 8.30 September 2019 MKFIFO(1)