bash 如何测试命名管道是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20245675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:47:46  来源:igfitidea点击:

How to test if a named pipe exists?

bash

提问by psihodelia

In order to test if a file exists I do:

为了测试文件是否存在,我执行以下操作:

if [ -f $FILE ];

but it doesn't work if $FILE is a named pipe, e.g. ls -l pipenameshows a pipe with pattribute:

但如果 $FILE 是命名管道,则它不起作用,例如ls -l pipename显示具有p属性的管道:

prw-r--r-- 1 usr grp 0 Nov 26 02:22 pipename

How to test if a named pipe exists?

如何测试命名管道是否存在?

回答by iruvar

You could use the -ptest

你可以使用-p测试

if [[ -p $pipe ]]

or

或者

if [ -p "$pipe" ]

回答by psihodelia

The friendly man page lists several file test operators, including:

友好的手册页列出了几个文件测试操作符,包括:

-e file
       True if file exists.

and

-f file
       True if file exists and is a regular file.

and

-p file
       True if file exists and is a named pipe (FIFO).

Don't just use -fall the time; use the one that does the thing you mean.

不要一直使用-f;使用可以完成您的意思的那个。

回答by Rahul Tripathi

You can try like this:

你可以这样试试:

if [[  -p $pipe ]]

or you can simply try by removing the []like this:

或者你可以简单地尝试删除[]这样的:

if [ -p "$pipe" ]

Also check Bash Conditional Expressionsand Bash Shell: Check File Exists or Not

还要检查Bash 条件表达式Bash Shell:检查文件是否存在