For a fifo stack, the modified version of your logic would be something like the below script. Naturally, you would need to modify that to allow passing the desired line as a parameter on the command line (or as redirected input).
As for removing the first/last line from the stack/buffer file from the file in-place, that would be a task for sed.
I was able to identify the logic for deleting the first/last line depending on fifo/lifo mode using very simple sed directives. That is implemented in the below script.
#!/bin/bash
Push=0
Pop=0
fifo=1 ; mode="fifo" ### DEFAULT
lifo=0
while [ $# -gt 0 ]
do
case $1 in
--push ) Push=1 ; Pop=0 ; shift ;;
--pop ) Pop=1 ; Push=0 ; shift ;;
--fifo ) fifo=1 ; lifo=0 ; mode="fifo" ; shift ;; ### Buffer MODE
--lifo ) lifo=1 ; fifo=0 ; mode="lifo" ; shift ;; ### Stack MODE
* ) echo -e "\n Invalid parameter used on command line. Only valid options: [ --push | --pop ]\n Bye!\n" ; exit 1 ;;
esac
done
if [ ${Push} -eq 0 -a ${Pop} -eq 0 ]
then
echo -e "\n ERROR: must specify one of the two stack operations: --push or --pop \n Bye!\n" ; exit 1
fi
stack="${mode}.txt";
tmp="${mode}.tmp"
if [ ! -f "${stack}" ] ; then touch "${stack}" ; fi
i=$(( $(wc -l "${stack}" | awk '{ print $1 }' ) + 1 ))
if [ ${Push} -eq 1 ]
then
# put a new line into FIFO/LIFO
echo "append to FIFO - line ${i}" >> "${stack}"
wc -l "${stack}"
fi
if [ ${Pop} -eq 1 ]
then
if [ -s "${stack}" ]
then
#get line back from FIFO
if [ ${fifo} -eq 1 ]
then
head -n1 "${stack}"
sed --in-place '1d' "${stack}"
wc -l "${stack}"
else
tail -n1 "${stack}"
sed --in-place '$d' "${stack}"
wc -l "${stack}"
fi
else
echo -e "\t ERROR: attempt to pop line from EMPTY stack.\n" >&2
exit 1
fi
fi
Session log is as follows:
me@OasisMega1:/0__WORK$ ./test_129.sh --push -fifo
1 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --push -fifo
2 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --push -fifo
3 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --push -fifo
4 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --pop -fifo
append to FIFO - line 1
3 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --pop -fifo
append to FIFO - line 2
2 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --pop -fifo
append to FIFO - line 3
1 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --pop -fifo
append to FIFO - line 4
0 fifo.txt
me@OasisMega1:/0__WORK$ ./test_129.sh --pop -fifo
ERROR: attempt to pop line from EMPTY stack.
me@OasisMega1:/0__WORK$
Session log for LIFO mode is as follows:
me@OasisMega1:0__WORK$ ./test_129.sh --push --lifo
1 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --push --lifo
2 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --push --lifo
3 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --push --lifo
4 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --pop --lifo
append to FIFO - line 4
3 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --pop --lifo
append to FIFO - line 3
2 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --pop --lifo
append to FIFO - line 2
1 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --pop --lifo
append to FIFO - line 1
0 lifo.txt
me@OasisMega1:0__WORK$ ./test_129.sh --pop --lifo
ERROR: attempt to pop line from EMPTY stack.
me@OasisMega1:0__WORK$
This version of the script incorporates providing input for buffer/stack on command line, and option for display of current buffer/stack contents. Also provides return code which might be used for decision on whether contents requiring "processing" are still in buffer or not.
#!/bin/bash
Push=0
Pop=0
Display=0
fifo=1 ; mode="FIFO" ### DEFAULT
lifo=0
dataline=""
while [ $# -gt 0 ]
do
case $1 in
--push ) Push=1 ; Pop=0 ; shift ;;
--pop ) Pop=1 ; Push=0 ; shift ;;
--show ) Push=0; Pop=0 ; Display=1 ; shift ;;
--fifo ) fifo=1 ; lifo=0 ; mode="FIFO" ; shift ;; ### Buffer MODE
--lifo ) lifo=1 ; fifo=0 ; mode="LIFO" ; shift ;; ### Stack MODE
-- ) shift ; dataline=${@} ; break ;;
* ) echo -e "\n Invalid parameter used on command line. Only valid options: [ --push | --pop ]\n Bye!\n" ; exit 1 ;;
esac
done
if [ ${Display} -eq 1 ]
then
if [ ${fifo} -eq 1 ]
then
if [ -f FIFO.txt ]
then
if [ -s FIFO.txt ]
then
wc -l FIFO.txt >&2
echo -e "Hit return to view contents ...\c" >&2 ; read k <&2
more FIFO.txt
RC=98
else
echo -e "\n FIFO buffer is empty at this time.\n" >&2
RC=0
fi
else
echo -e "\n FIFO buffer does not exist.\n" >&2
RC=99
fi
fi
if [ ${lifo} -eq 1 ]
then
if [ -f LIFO.txt ]
then
if [ -s LIFO.txt ]
then
wc -l LIFO.txt >&2
echo -e "Hit return to view contents ...\c" >&2 ; read k <&2
more LIFO.txt
RC=98
else
echo -e "\n LIFO buffer is empty at this time.\n" >&2
RC=0
fi
else
echo -e "\n LIFO buffer does not exist.\n" >&2
RC=99
fi
fi
exit ${RC}
fi
if [ ${Push} -eq 1 -a -z "${dataline}" ]
then
echo -e "\n ERROR: did not provide data for capture in BUFFER/STACK\n Bye!\n" ; exit 1
fi
if [ ${Push} -eq 0 -a ${Pop} -eq 0 ]
then
echo -e "\n ERROR: must specify one of the two stack operations: --push or --pop \n Bye!\n" >&2 ; exit 1
fi
stack="${mode}.txt";
tmp="${mode}.tmp"
if [ ! -f "${stack}" ] ; then touch "${stack}" ; fi
if [ ${Push} -eq 1 ]
then
# put a new line into FIFO/LIFO
echo "${dataline}" >> "${stack}"
wc -l "${stack}" >&2
fi
if [ ${Pop} -eq 1 ]
then
if [ -s "${stack}" ]
then
#get line back from FIFO
if [ ${fifo} -eq 1 ]
then
head -n1 "${stack}"
sed --in-place '1d' "${stack}"
wc -l "${stack}" >&2
else
tail -n1 "${stack}"
sed --in-place '$d' "${stack}"
wc -l "${stack}" >&2
fi
else
echo -e "\t ERROR: attempt to pop line from EMPTY stack.\n" >&2
exit 1
fi
fi
dd
; you've got the startup time for that tool itself to pay, but it avoids needing to rewrite all the data when popping off the beginning)