Skip to content

Instantly share code, notes, and snippets.

@raphtlw
Created August 11, 2021 08:11
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 raphtlw/233a591330312b8e715ca2eaa4a46a88 to your computer and use it in GitHub Desktop.
Save raphtlw/233a591330312b8e715ca2eaa4a46a88 to your computer and use it in GitHub Desktop.
Bash script template with argument parsing
#!/usr/bin/env bash
# ----------------------------------------------------------------------------
# script.sh: Bash script template with argument parsing
#
# Author: Raphael Tang <raphpb1912@gmail.com>
# ----------------------------------------------------------------------------
# Dependencies:
# - dependency1
# - dependency2
CLEAR='\033[0m'
RED='\033[0;31m'
function usage() {
if [ -n "$1" ]; then
echo -e "${RED}👉 $1${CLEAR}\n";
fi
cat << EOF
Usage: $(basename $0) [REQUIRED -u url] [-n]
-h, --help Show this message
-u, --url (Required) URL
-n, --nocaca Won't display colored output, uses aalib instead
Examples: $(basename $0) --nocaca -u "https://www.youtube.com/watch?v=5qap5aO4i9A"
EOF
}
# parse params
while [[ "$#" > 0 ]]; do case $1 in
-h|--help) usage; exit 0 ;;
-u|--url) URL="$2"; shift; shift ;;
-n|--nocaca) NOCACA=1; shift ;;
*) usage "Unknown parameter passed: $1"; exit 1 ;;
esac; done
if [ -z "$URL" ]; then
usage "No URL specified"
exit 1
fi
if [ -n "$NOCACA" ]; then
youtube-dl "$URL" -f worst -o - | mplayer -vo aa -monitorpixelaspect 0.5 -
else
streamlink "$URL" worst --player mpv -a "--vo=caca"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment