Skip to content

Instantly share code, notes, and snippets.

@rragundez
Created June 21, 2017 09:11
Show Gist options
  • Save rragundez/40025e94b91c147a709b8dda300d5b5f to your computer and use it in GitHub Desktop.
Save rragundez/40025e94b91c147a709b8dda300d5b5f to your computer and use it in GitHub Desktop.
Template of bash script with mandatory and optional arguments
#!/bin/bash
set -e
usage="$(basename "$0") [-h] [-i PROJECT] [-v VM] [-p PYTHON] [-d NOTEBOOKS]
Make a user provide SSH key and jupyter notebooks (in roles/bootstrap/files/notebooks) to each user listed in var/common.yml
where:
-h show this help text
-i google cloud project id
-v name of instance/virtual machine
-p python path
-d if want to copy a notebooks directory"
# constants
PYTHON=/usr/bin/python3
NOTEBOOK_DIR=false
options=':hi:v:p:d:'
while getopts $options option; do
case "$option" in
h) echo "$usage"; exit;;
i) PROJECT=$OPTARG;;
v) VM=$OPTARG;;
p) PYTHON=$OPTARG;;
d) NOTEBOOK_DIR=$OPTARG;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2; echo "$usage" >&2; exit 1;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2; echo "$usage" >&2; exit 1;;
esac
done
# mandatory arguments
if [ ! "$PROJECT" ] || [ ! "$VM" ]; then
echo "arguments -i and -v must be provided"
echo "$usage" >&2; exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment