# pushpop v1.0 -rw-r--r-- 1.3 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
# pushpop - a mental stack manager
# Copyright (C) 2020 Erazem Kokot <contact at erazem dot eu>
# Inspired by http://www.secretgeek.net/pushpop
# The stack file is in reverse order (file bottom = stack top)

STACK="${PWD}"/stack.txt
HIST="${PWD}"/hist.txt
VERSION=1.0

usage() {
	echo "Usage: $(basename $0) [option] (item)"
	echo "    -h, --help      Show this message"
	echo "    -v, --version   Show version info"
	echo "    --push (item)   Add the item to the top of the stack"
	echo "    --pop           Remove the topmost item from the stack"
	echo "    --peek          Show the topmost item in the stack"
	echo "    -s, --snoop     Show all the items in the stack"
	echo "    -n, --size      Show the number of all items on the stack"
	echo "    --history       Show all completed items"
}

if [ $# -lt 1 ]; then
	usage;
	exit 1;
else
	case "$1" in
		-h|--help) usage; exit 0 ;;
		-v|--version) echo "$(basename $0) v${VERSION}"; exit 0 ;;
		--push) shift; echo "$1" >> "${STACK}" ;;
		--pop) tail -n 1 < "${STACK}" >> "${HIST}" && \
			sed -i '$d' "${STACK}" ;;
		--peek) tail -n 1 < "${STACK}" ;;
		-s|--snoop) tac < "${STACK}" ;;
		-n|--size)  wc -l < "${STACK}" ;;
		--history) [ -f "${HIST}" ] && tac < "${HIST}" || echo "No history" ;;
		*) usage; exit 1 ;;
	esac
fi