The problem with your setup is that you have the creation of fifo in the wrong script if you want to control access to the fifo file while the program is running. To fix the problem, you need to do something like this:
reader: fifo_read.sh
#!/bin/bash fifo_name="/tmp/myfifo" # fifo name trap "rm -f $fifo_name" EXIT # set trap to rm fifo_name at exit [ -p "$fifo_name" ] || mkfifo "$fifo_name" # if fifo not found, create exec 3< $fifo_name # redirect fifo_name to fd 3 # (not required, but makes read clearer) while :; do if read -r -u 3 line; then # read line from fifo_name if [ "$line" = 'quit' ]; then # if line is quit, quit printf "%s: 'quit' command received\n" "$fifo_name" break fi printf "%s: %s\n" "$fifo_name" "$line" # print line read fi done exec 3<&- # reset fd 3 redirection exit 0
writer: fifo_write.sh
#!/bin/bash fifo_name="/tmp/myfifo" # Se non esiste, exit :); [ -p "$fifo_name" ] || { printf "\n Error fifo '%s' not found.\n\n" "$fifo_name" exit 1 } [ -n "$1" ] && printf "%s\n" "$1" > "$fifo_name" || printf "pid: '%s' writing to fifo\n" "$$" > "$fifo_name" exit 0
: (start reading in terminal 1)
$ ./fifo_read.sh
(startup script in the second terminal)
$ ./fifo_write.sh "message from writer"
output in terminal 1:
$ ./fifo_read.sh /tmp/myfifo: message from writer /tmp/myfifo: pid: '28698' writing to fifo /tmp/myfifo: 'quit' command received
David C. Rankin
source share