-
-
Save thingsiplay/69aada6a57f829d9580d7b54f8a207a0 to your computer and use it in GitHub Desktop.
cheat.sh - The only cheat sheet you need
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
#!/bin/env bash | |
cheat='curl -s cheat.sh' | |
menu='fzf --reverse' | |
pager='less -R -c' | |
cachefile_max_age_hours=6 | |
# Path to temporary cache file. If your Linux system does not support /dev/shm | |
# or if you are on MacOS, then change the path to your liking: | |
cachefile='/dev/shm/cheatlist' # GNU+LINUX | |
# cachefile="${TMPDIR}/cheatlist" # MacOS/Darwin | |
# Download list file and cache it. | |
listing () { | |
if [ -f "${cachefile}" ] | |
then | |
local filedate=$(stat -c %Y -- "${cachefile}") | |
local now=$(date +%s) | |
local age_hours=$(( (now - filedate) / 60 / 60 )) | |
if [[ "${age_hours}" > "${cachefile_max_age_hours}" ]] | |
then | |
${cheat}/:list > "${cachefile}" | |
fi | |
else | |
${cheat}/:list > "${cachefile}" | |
fi | |
cat -- "${cachefile}" | |
} | |
case "${1}" in | |
'') | |
if selection=$(listing | ${menu}) | |
then | |
${cheat}/"${selection}" | ${pager} | |
fi | |
;; | |
'-h') | |
${cheat}/:help | ${pager} | |
;; | |
'-l') | |
listing | |
;; | |
*) | |
${cheat}/${@} | ${pager} | |
;; | |
esac |
This case statement would allow the script to work on MacOS as well as Linux.
case "$(uname -o)" in
GNU/Linux) cachefile='/dev/shm/cheatlist' ;;
Darwin) cachefile="$TMPDIR/cheatlist" ;;
*) echo "Unsupported OS"; exit 1 ;;
esac
Thank you for the suggestion. I actually would like to avoid additional checks that are always true for the current user. The variables are intended to be changed for the environment and user needs, like an option. So I will add a comment and a line ready to uncomment instead. I prefer this approach. I hope this is a good enough compromise.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to give a special thanks to the r/bash community. This little script is shaped strongly by the suggestions made at https://www.reddit.com/r/bash/comments/13xz1kc/the_only_cheat_sheet_you_need/ .