Skip to content

Instantly share code, notes, and snippets.

@jpsutton
Last active August 25, 2022 15:55
Show Gist options
  • Save jpsutton/6cc6396cb2e90ce271dc3ffae7c0e047 to your computer and use it in GitHub Desktop.
Save jpsutton/6cc6396cb2e90ce271dc3ffae7c0e047 to your computer and use it in GitHub Desktop.
CLI interface to explainshell.com
#!/usr/bin/env sh
#
# Explain a given shell command using explanation from explainshell.com
#
# Usage: explainshell <command>
#
# Credit: Derived from https://github.com/benjamine/explain.sh
# License: MIT
URL="$*"
for prereq in curl lynx sed xmllint; do
which "$prereq" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR: \`${prereq}\` is not available in your current \$PATH; install the application before using this tool" > /dev/stderr
exit 1
fi
done
if [ -z "$URL" ]; then
echo "Usage: explainshell.sh <command>"
exit
fi
# lame url encoding
URL=$(echo "$URL" | sed -e 's/%/%25/g')
URL=$(echo "$URL" | sed -e 's/ /%20/g')
URL=$(echo "$URL" | sed -e 's/!/%21/g')
URL=$(echo "$URL" | sed -e 's/"/%22/g')
URL=$(echo "$URL" | sed -e 's/#/%23/g')
URL=$(echo "$URL" | sed -e 's/\$/%24/g')
URL=$(echo "$URL" | sed -e 's/\&/%26/g')
URL=$(echo "$URL" | sed -e 's/'\''/%27/g')
URL=$(echo "$URL" | sed -e 's/'\('/%28/g')
URL=$(echo "$URL" | sed -e 's/'\)'/%29/g')
URL=$(echo "$URL" | sed -e 's/\*/%2a/g')
URL=$(echo "$URL" | sed -e 's/+/%2b/g')
URL=$(echo "$URL" | sed -e 's/,/%2c/g')
URL=$(echo "$URL" | sed -e 's/-/%2d/g')
URL=$(echo "$URL" | sed -e 's/\./%2e/g')
URL=$(echo "$URL" | sed -e 's/\//%2f/g')
URL=$(echo "$URL" | sed -e 's/:/%3a/g')
URL=$(echo "$URL" | sed -e 's/;/%3b/g')
URL=$(echo "$URL" | sed -e 's/>/%3e/g')
URL=$(echo "$URL" | sed -e 's/?/%3f/g')
URL=$(echo "$URL" | sed -e 's/@/%40/g')
URL=$(echo "$URL" | sed -e 's/\[/%5b/g')
URL=$(echo "$URL" | sed -e 's/\\/%5c/g')
URL=$(echo "$URL" | sed -e 's/\]/%5d/g')
URL=$(echo "$URL" | sed -e 's/\^/%5e/g')
URL=$(echo "$URL" | sed -e 's/_/%5f/g')
URL=$(echo "$URL" | sed -e 's/`/%60/g')
URL=$(echo "$URL" | sed -e 's/{/%7b/g')
URL=$(echo "$URL" | sed -e 's/|/%7c/g')
URL=$(echo "$URL" | sed -e 's/}/%7d/g')
URL=$(echo "$URL" | sed -e 's/~/%7e/g')
URL="https://www.explainshell.com/explain?cmd=${URL}"
table_blob=$(curl "$URL" 2>/dev/null | xmllint --html --xpath "//table[@id=\"help\"]" /dev/stdin 2>/dev/null)
html_blob='<html><body>'"${table_blob}"'</body></html>'
echo "$html_blob" | lynx --stdin --dump | sed 's/^ //g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment