I want to write to a named pipe only if it already has a reader. Currently, I'm using
timeout to detect whether the attempt to write to the pipe blocked, like so:
#! /usr/bin/env bash rm -f pipe mkfifo pipe sleep 5 timeout 1 bash -c "echo Hello > pipe" if [[ $? == 0 ]] then echo Somebody got our message else echo Nobody read from pipe, so we didn\'t send a message fi
This works. If I
tail -f pipe in a separate terminal during the sleep I get one message, and if I don't, I get the other. But is there a better way? Ideally it would be something that doesn't rely on a timeout.