Last active
May 19, 2024 05:15
-
-
Save mekb-turtle/da8229201df6df68a54a023300a93b2c to your computer and use it in GitHub Desktop.
Explain a command using explainshell.com
This file contains 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/bash | |
explain() { | |
if [[ "$#" == 0 ]]; then | |
echo "Usage: explain <command>..." | |
echo "Explain a command using explainshell.com" | |
echo "Example: explain 'ls -l -a'" | |
echo "For best results, quote the command to prevent shell expansion" | |
return 1 | |
fi | |
if ! type jq &>/dev/null; then | |
echo "This script requires jq to parse the input" >&2 | |
echo "Please install jq and try again" >&2 | |
return 2 | |
fi | |
if ! type lynx &>/dev/null; then | |
echo "This script requires lynx to fetch the explanation" >&2 | |
echo "Please install lynx and try again" >&2 | |
return 2 | |
fi | |
# Loop through each input | |
local input escaped r | |
for input in "$@"; do | |
# Parse the input and encode it for the URL using jq | |
escaped="$(printf "%s" "$input" | jq -srR @uri)" | |
# Fetch the explanation using lynx | |
lynx "https://explainshell.com/explain?cmd=${escaped}" \ | |
-dump -width=160 -nolist | | |
tail -n +9 # Trim off the site header | |
r="$?" | |
echo | |
done | |
return "$r" | |
} | |
explain "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment