Skip to content

Instantly share code, notes, and snippets.

@chernjie
Last active September 12, 2020 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernjie/911cac97adc0531be3dc18ceee6b86a5 to your computer and use it in GitHub Desktop.
Save chernjie/911cac97adc0531be3dc18ceee6b86a5 to your computer and use it in GitHub Desktop.
CLI implementation of Set, using file for storage
#!/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