5

I tried using the -f flag to test if a named pipe is present

if [[ ! -f "$fifo" ]]; then
  echo 'There should be a fifo.lock file in the dir.' > /dev/stderr
  return 0;
fi

this check does not seem correct. So perhaps a named-pipe is not a file, but something else?

1 Answers1

5

You need to use the -p construct to see if the file is of type named pipe. It works with the standard test [ (POSIX compliant) and the extended test operators [[ ( bash/zsh specific )

if [[ -p "$fifo" ]]; then
    printf '%s is a named pipe' "$fifo"
fi

From the man pages of bash

-p file

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

or use the file command with the -b to just print the type information without the filename. The -b might not be POSIX compliant

if [ $(file -b "$fifo") = "fifo (named pipe)" ]; then
   printf '%s is a named pipe' "$fifo"
fi

Without the -b, one could do

type=$(file "$fifo")
if [ "${type##*: }" = "fifo (named pipe)" ]; then 
Inian
  • 12,472
  • 1
  • 35
  • 52