I am looking for a portable bash solution for a simple FIFO queue. My queue input will always be a single line of text.I came up with a simple solution, which works well.
fifo_queue="fifo.txt";
fifo_tmp="fifo.tmp"
# put a new line into FIFO
echo "append to FIFO a line" >> $fifo_stack;
#get line back from FIFO
echo $(head -n1 "$fifo_queue")
tail -n +2 "$fifo_queue" > "$fifo_tmp" && mv "$fifo_tmp" "$fifo_queue"
Now the question: Is there a more elegant (i.e. easy understandable) way to retrieve the FIFO line. For example with a simple one-liner instead of two lines?
If you want to edit a file, use an editor; don't cobble something together with tail
.
put () {
printf '%s\n' "$1" >> "$2"
}
get () {
printf '1p\n1d\nwq\n' | ed -s "$1"
}
Then
$ put a queue.txt
$ put b queue.txt
$ get queue.txt
a
$ put c queue.txt
$ get queue.txt
b