Cheatsheet script for displaying Linux command examples (gist.github.com)
from thingsiplay@beehaw.org to linux@lemmy.ml on 13 Aug 2024 11:14
https://beehaw.org/post/15501425

You can use cheat sh web service to show cheatsheets for all kind of commands. Just replace the command name: curl -s cheat.sh/date. I also wrote a a simple script with filename being just a question mark to get a working command as ?, that shows all commands in fzf menu if no argument is given or shows the cheatsheet in the less pager if command name is given.

Usage:

?
? -l
? date
? grep

Script ?:

#!/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

#linux

threaded - newest

original_reader@lemm.ee on 14 Sep 2024 16:29 collapse

This looks great.

Suggestion: a step-by-step “howto” with an example or three to make it more useful for beginners.

thingsiplay@beehaw.org on 14 Sep 2024 19:17 collapse

Thanks. Is this suggestion towards this ? script or the output from the cheat.sh web service? Because I’m not the author of the web service itself, I just created this script to make use of it.