Last active
September 12, 2020 21:43
-
-
Save chernjie/911cac97adc0531be3dc18ceee6b86a5 to your computer and use it in GitHub Desktop.
CLI implementation of Set, using file for storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
STATEFILE=${STATEFILE:-retry.log} | |
# dependencies | |
for i in sort gsed grep echo | |
do command -v $i > /dev/null || (echo $i not found >&2 && exit 1) | |
done | |
prime() { | |
printf '' > $STATEFILE | |
} | |
add() { | |
if test $# -eq 0 | |
then | |
sort --unique >> $STATEFILE | |
else | |
echo "$@" >> $STATEFILE | |
fi | |
} | |
remove() { | |
gsed --in-place "/${*//\//.}/d" $STATEFILE | |
} | |
has() { | |
grep --quiet "${*//\//.}" $STATEFILE | |
} | |
getAll() { | |
sort --unique $STATEFILE | grep -ve ^$ -ve ^# | |
} | |
case $1 in | |
prime) "$@" ;; | |
add) "$@" ;; | |
remove) "$@" ;; | |
has) "$@" ;; | |
getAll) "$@" ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment