Skip to content

Instantly share code, notes, and snippets.

@tchajed
Last active June 26, 2024 14:52
Show Gist options
  • Save tchajed/4e65690f29866e2dc2b4da5b6d5af43b to your computer and use it in GitHub Desktop.
Save tchajed/4e65690f29866e2dc2b4da5b6d5af43b to your computer and use it in GitHub Desktop.
Starting a bash script
#!/bin/bash
# basic manual argument parsing
usage() {
echo "Usage: $0 [--foo FOO] [-v]" 1>&2
}
foo=""
verbose=false
while [[ $# -gt 0 ]]; do
case "$1" in
--foo)
shift
foo="$1"
shift
;;
-v | --verbose)
shift
verbose=true
;;
-help)
usage
exit 0
;;
-*)
usage
exit 1
;;
*)
break
;;
esac
done
#!/bin/bash
# set -e exit whenever a command errors
# set -u fails when a variable is undefined (including arguments $1, etc.)
# set -o pipefail fails a pipe when any command fails (this isn't always a good idea)
set -euo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
blue=$(tput setaf 4 || printf "")
green=$(tput setaf 2 || printf "")
red=$(tput setaf 1 || printf "")
reset=$(tput sgr0 || printf "")
info() {
echo -e "${blue}$1${reset}"
}
error() {
echo -e "${red}$1${reset}" 1>&2
}
good() {
echo -e "${green}$1${reset}" 1>&2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment